Ruby from another language

Habits that carry over

If you already write Python, Perl or Java, most of what you know is still true in Ruby: values have types, collections hold them, a function takes arguments and returns a result. What changes is the spelling. This lesson is a map from the constructs you own to the Ruby ones, so the first script does not feel like a new alphabet.

Start with a list and a transformation. Python writes a comprehension, Java a loop or a stream, Ruby a method with a block:

numbers = [1, 2, 3] doubled = numbers.map { |n| n * 2 } puts doubled.inspect

ruby -e 'puts [1, 2, 3].map { |n| n * 2 }.inspect'

[2, 4, 6]

The same idea in Ruby

The table is the lesson. The left column is a habit from another language; the right column is the Ruby you write instead.

HabitRuby
Python [n * 2 for n in nums]nums.map { |n| n * 2 }
Python [n for n in nums if n % 2 == 0]nums.select(&:even?)
Java list.get(0)list[0], negative from the end: list[-1]
Python def f(a, b=2)def f(a, b = 2)
Perl $hash{key}hash[:key] for a symbol, hash["key"] for a string
Java System.out.println(x)puts x
Python None, Java nullnil
C-style for (i = 0; i < n; i++)n.times { |i| } or (0...n).each { |i| }

A few of these run as one-liners, and the output is the same shape you would get in Python:

ruby -e 'p (1..5).select(&:even?)'

[2, 4]

ruby -e 'p [3, 1, 2].sort'

[1, 2, 3]

ruby -e 'name = "Ada"; puts "Hello, #{name}"'

Hello, Ada

Where the habits break

Three habits from other languages cause real bugs in Ruby, and all three are quick to unlearn:

The rest of the course teaches each of these where it matters. If you want the shortest bridge from Python, lessons 13.21 and 13.22 put the same program in both languages side by side.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is the Ruby spelling of the Python comprehension [n * 2 for n in nums]?

    Answer

    nums.map { |n| n * 2 }

    map takes a block and returns the array of the block results.

  2. What does this program print? numbers = [1, 2, 3] doubled = numbers.map { |n| n * 2 } puts doubled.inspect

    Answer

    [2, 4, 6]

    map doubles each element, and inspect writes the array with its brackets and commas.

  3. Which Ruby call keeps the even numbers of an array?

    Answer

    nums.select(&:even?)

    select keeps the elements for which the block returns true; map would return booleans.

  4. How do you read the last element of an array in Ruby?

    Answer

    list[-1]

    A negative index counts from the end, so -1 is the last element.

  5. What does this program print? p (1..5).select(&:even?)

    Answer

    [2, 4]

    The range is filtered to its even numbers, and p prints the array.

  6. What replaces Python None and Java null in Ruby?

    Answer

    nil

    nil is the one "no value" object, and it is also falsy.

  7. Which two values are falsy in Ruby?

    Answer

    false and nil

    Only false and nil fail a condition; all numbers, strings and arrays pass it.

  8. What does the C-style loop for (i = 0; i < n; i++) become in Ruby?

    Answer

    n.times { |i| }

    times calls the block n times with 0 up to n - 1, which is the same count.

  9. What does 2.zero? show about Ruby?

    Answer

    Every value is an object, including an integer

    An integer is an object, so it has methods such as class and zero?.

  10. What does this program print? count = 0 puts "zero" if count

    Answer

    zero

    Zero is truthy, so the condition passes where a Python habit would expect it to fail.

Article author: Arthur Isaev