Other Pages

Expand All

ruby_language.step

goals do
  goal "Be able to use the basic building blocks of Ruby code"
  goal "Use IRB to run Ruby code"
  goal "Do simple calculations"
  goal "Use and understand variables"
  goal "Use and understand arrays"
  goal "Use loops and conditional statements"
end

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

    console_without_message "irb"

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

    console_without_message "irb(main):001:0>"
  end

  step do
    console_with_message "Next try some simple math that's built into Ruby. Type these lines into IRB:", "3 + 3\n7 * 6"
  end

  step do
    message "**Variables** are names with values assigned to them."

    console_without_message "my_variable = 5"

    message "This assigns the value `5` to the name `my_variable`."
  end

  step do
    message "You can also do math with variables:"
    console_without_message <<-RUBY
my_variable + 2
my_variable * 3
    RUBY
  end

  step do
    message "Variables can also hold more than one value. This is called an **array**."

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

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

  step do
    console <<-RUBY
fruits = fruits + ["orange"]
fruits = fruits - ["kiwi"]
    RUBY

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

  end

  step do
    message "Everything in Ruby has a **class**. Type this into IRB:"

    console_without_message <<-RUBY
7.class
"kiwi".class
fruits.class
RUBY

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

  step do
    message "Each class has different **methods** that can be used on **instances** of that class."

    console_without_message <<-RUBY
fruits.length
fruits.first
    RUBY

    message "You can see all the methods available for an object:"
    console_without_message <<-RUBY
fruits.methods
    RUBY

    message "And **chain** methods together:"
    console_without_message <<-RUBY
fruits.methods.sort
    RUBY
  end

  step do
    message "Arrays have a method called **each** which iterates through the list running code on each item."
    console_without_message <<-RUBY
fruits.each do |fruit|
  puts fruit
end
    RUBY
    message "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."
  end

  step do
    message "A **conditional** runs code only when a statement evaluates to true."

    console_without_message <<-RUBY
if my_variable > 1
  puts "YAY!"
end
    RUBY

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

    message "Try changing the `>` in the conditional to a `<`."

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

    console_without_message <<-RUBY
if my_variable > 1
  puts "YAY!"
else
  puts "BOO!"
end
    RUBY
  end

  step do
    message "You can also make your own methods:"
    console_without_message <<-RUBY
def pluralize(word)
  word + "s"
end
pluralize("kiwi")
    RUBY

    message "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."

    message "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."
  end

  step do
    message "Putting it all together, let's make a method that says your opinion of some fruits:"
    message "**Don't try to type this all in!** Just paste it into irb and see what happens."
    console_without_message <<-RUBY
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"])
    RUBY

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

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

next_step "getting_started"