week 4
Tags: ruby, devjournal, codingbootcamp
Published: 2020-01-05 20:40:50

      Week 4

Coming up on finishing our first month of the bootcamp, we finally began our learning of Ruby. Ruby is the nicest language so far. Why is that? Because of MINASWAN. Matz is nice and so we are nice.

Ruby is object oriented.Everything in Ruby is an object, dynamically typed, a scripting language, and written line by line.

To simply display some text or integers in Ruby you can use 'puts', 'prints', or 'p' followed by your text or so in single quotes. If you were to pass punctuation in Ruby, you would use double quotes = "". String interpolation looks like "#{}" and Arrays appear the same as they would in JavaScript.

Ruby has a lot of methods that can be applied. Some of these methods we learned include:

lets use this as our example:

 nums = [ 13, 5, 17, 4, 10, 5 ]

.length (will display the length of the array)

  puts nums.length -> 6

.first (will display the first variable)

 puts nums.first -> 13

.last (will display the last variable)

 puts nums.last -> 5

.include? (returns a boolean regarding of that variable is included)

 puts nums.include?4 -> true
 puts nums.include?6 -> false

.reverse (will reverse the order)

 puts nums.reverse -> [ 5, 10, 4, 17, 5, 13 ]


There is another type of methods in Ruby called mutator methods. Mutator methods change one or more of the variables in an object. Mutator methods are also known as setter methods. We can use the shovel << method to add variables to an already existing object.

Conditionals in Ruby are similar to JavaScript in terms of needing an if else and end. However, in Ruby the format is a little different. We will be using if, else, elsif, and end.

Now lets get into classes and inheritance. Some of the syntax and language we will be using is capitalizing the first letter of the class name, using 'def' to define our method, using '_' rather than camelCase, and using @ for our instance variables.

Lets take a peak at a simple Ruby code with class:

```
class Bike
 def set_color(new_color)
  @color = new_color
 end
 def describe
  "This bike is the color #{color}"
 end
end

bike = Bike.new
bike.set_color("Red")

puts bike.describe

#this will return " This bike is the color Red
```

Next topic in Ruby was class inheritance. As the internet likes to say, "in object-oriented programming, inheritance enables new objects to take on the properties of existing objects". Similar to React components, we have Ruby inheritance.

To inherit from another class we use the less than symbol showing one class taking from another like so :
```
class Apple < Fruit
```
We can apply this to our Bike class by creating multiple classes of different bike style. With multiple instance variables starting off in our Bike class, we can manipulate those variables in each of our new classes depending on our bike. 

Lets take a peak of what this code would look like using a fruit example:
```
class Fruit

 def initialize color
  @color = color
 end

 def color
  @color
 end

 def is_sweet
  true
 end

end

class Apple < Fruit
 
 def initialize color
  super color + "Apple"
 end

 def spoils
  "Spoils in 7 days"
 end

end

apple_one = Apple.new("Red")
apple_one.color

-> "Red Apple"

apple_one.ist_sweet

-> true

apple_one.spoils

-> "Spoils in 7 days"

```

Some of the challenges we did this week included a Ruby car challenge, text based story game, and creating a task list. We also spent a day doing test driven development, TDD, in Ruby which is called RSPEC. We started by writing out our expectations for the code, running our code to fail, going back into our code writing just enough to pass, and then continuing with our testing until we have a completed code.

Ruby to me is a cleaner and easier to read form of JavaScript. It was a great week and a great way to finish out our first month

Checking out!