Other Pages

Ruby Language

Goals

  • Be able to use the basic building blocks of Ruby code

  • Use IRB to run Ruby code

  • Do simple calculations

  • Use and understand variables

  • Use and understand arrays

  • Use loops and conditional statements

Steps

Step 1

Type this in the terminal to start the Interactive Ruby Shell, a program which lets you try out Ruby code:

irb

Yours might look different, but it should look something like this:

3.0.3 :001 >

Step 2

Next try some simple math that's built into Ruby. Type these lines into IRB:
3 + 3
7 * 6

Step 3

Variables are names with values assigned to them.

my_number = 5
my_fruit = 'kiwi'

This assigns the number 5 to the name my_number and the text kiwi to the variable my_fruit. You can make up as many variables as you like.

Step 4

You can also do math with variables:

my_variable + 2
my_variable * 3

Step 5

Variables can also hold more than one value. This is called an array.

fruits = ["kiwi", "strawberry", "plum"]

Here we're using the variable fruits to hold a collection of fruit names.

Step 6

Type this in the shell:
fruits = fruits + ["orange"]
fruits = fruits - ["kiwi"]

+ and - are called operators. We can use them with the array of fruits just like we can use them with numbers.

Step 7

Everything in Ruby has a class. Type this into IRB:

7.class
"kiwi".class
fruits.class

These are the three data types introduced so far: Fixnum (numbers), String (text), and Array (lists).

Step 8

Each class has different methods that can be used on instances of that class.

fruits.length
fruits.first

You can see all the methods available for an object:

fruits.methods

And chain methods together:

fruits.methods.sort

Step 9

Arrays have a method called each which loops through the list running code on each item.

fruits = ["kiwi", "strawberry", "plum"]
fruits.each do |fruit|
  puts fruit
end

This takes the first item from the fruits array, the value "kiwi", and assigns it to the variable fruit, and runs the code between do and end. Then it does the same thing for each other item in the list. The code above should print a list of the fruits.

Step 10

A conditional runs code only when a condition is met. In the following example the condition is my_number > 1

if my_number > 1
  puts "YAY!"
end

This prints YAY! if the value stored in my_number is greater than 1.

Try changing the > in the conditional to a <.

If you want to do something else when the condition is not met, you can use an else:

if my_number > 1
  puts "YAY!"
else
  puts "BOO!"
end

Step 11

You can also make your own methods:

def pluralize(word)
  word + "s"
end
pluralize("kiwi")
pluralize("apple")
pluralize("knife")

Methods take parameters, which are the variables they work on. In this case, we made a method called pluralize that takes one parameter, a word.

Methods can also return data. In this case, pluralize returns the word with an 's' added to the end of it. In Ruby, methods return whatever the last line of the method evaluates to.

Step 12

Putting it all together, let's make a method that gives some opinion of some fruits:

Don't try to type this all in! Just paste it into irb and see what happens.

def my_opinion(fruits)
  fruits.each do |fruit|
    if fruit == "pizza"
      puts "pizza is the best!!!"
    else
      puts pluralize(fruit) + " are pretty good, I guess..."
    end
  end
end
my_opinion(["apple", "pizza", "orange"])

Try changing this method to say what your favorite fruit is.

Before you move on to the next step you must exit IRB by typing 'exit'

Next Step: