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:

Option 1.1: Without Docker

irb

Option 1.2: With Docker

docker run -it --rm ruby:2.5.1-slim

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

irb(main):001:0>

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_variable = 5

This assigns the value 5 to the name my_variable.

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 terminal:
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

A hash is a dictionary-like collection of unique keys and their values.

Type this in the terminal:
fruit_dictionary = { strawberry: "a sweet soft red...", plum: "an oval, purple fruit...", orange: "a round juicy citrus fruit..." }

We're creating a hash which associates the name of a fruit (key) with the definition of the fruit (value).

Step 8

Type this in the terminal:
fruit_dictionary[:strawberry]

Here we're using the key, :strawberry, to look up the associated value, the definition of 'strawberry' in the fruit dictionary.

Step 9

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

7.class
"kiwi".class
fruits.class

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

Step 10

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 you can call multiple methods in a row:

fruits.methods.sort

In fruit.methods.sort above, fruit.methods returns an array, and .sort sorts that array. It's exactly like this, but without the array variable:

array = fruits.methods
array.sort

Step 11

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

fruits.each do |fruit|
  puts fruit
end

This takes the first item from the fruits array ("strawberry"), 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 12

A conditional runs code only when a statement evaluates to true.

if my_variable > 1
  puts "YAY!"
end

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

Try changing the > in the conditional to a <.

If you want to do something else when the statement evaluates to false, you can use an else:

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

Step 13

You can also make your own methods:

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

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 14

This is an optional small group practice question! Work in groups of 2-3 to solve the problem.

Write some Ruby that prints out the names of the people in your group.

Hints:

  1. Start by opening up irb.
  2. Create the names as strings in an array.
  3. Store that array to a variable.
  4. Then use the .each method on the stored array to loop through each of the names.
  5. Use the puts method to print out the names.

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

Next Step: