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.
| Habit | Ruby |
|---|---|
| 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 null | nil |
| 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:
- Only two values are falsy. false and nil; zero and the empty string are true. A Python habit of writing if items or if count breaks here: in Ruby an empty list and a zero count both pass the condition.
- Everything is an object. 2 + 2 is a method call on the integer 2, so 2.class and 2.zero? both work. There are no primitive values to treat differently.
- Parentheses are optional. puts x is a call, not a variable named puts x. Ruby resolves the ambiguity by the known method name, which is why puts(x) and puts x both work and why a local variable with a method name shadows the method.
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.
-
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.
-
What does this program print?
numbers = [1, 2, 3] doubled = numbers.map { |n| n * 2 } puts doubled.inspectAnswer
[2, 4, 6]
map doubles each element, and inspect writes the array with its brackets and commas.
-
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.
-
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.
-
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.
-
What replaces Python
Noneand Javanullin Ruby?Answer
nil
nil is the one "no value" object, and it is also falsy.
-
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.
-
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.
-
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?.
-
What does this program print?
count = 0 puts "zero" if countAnswer
zero
Zero is truthy, so the condition passes where a Python habit would expect it to fail.
Article author: Arthur Isaev