The same program in Ruby and Python

One program, two languages

This chapter takes one small task at a time and writes it twice: once in Ruby and once in Python. Both versions run in the image of this course, and the output below each one is the real run. The point is not to pick a winner; it is to see what each language makes easy, and what a reader coming from the other side should expect.

The first task is a greeting for a list of names. Ruby iterates with a block; Python iterates with a for loop and indentation.

Ruby

names = ["Ada", "Matz"] names.each { |name| puts "Hello, #{name}" }

ruby hello.rb

Hello, Ada
Hello, Matz

Python

names = ["Ada", "Matz"] for name in names: print(f"Hello, {name}")

python3 hello.py

Hello, Ada
Hello, Matz

Same output, four lines against three. The Ruby block is one expression: the list, the method and the body in braces. The Python loop is a statement that owns the lines under it, which is why the indentation is part of the syntax rather than a style choice.

Truthiness is not the same

The difference that bites first is not syntax; it is what counts as true. Ruby has exactly two falsy values: false and nil. Python has several, and 0 is one of them.

value = 0 puts "#{value} is truthy in Ruby" if value puts "done"

ruby truthy.rb

0 is truthy in Ruby
done

value = 0 if value: print(f"{value} is truthy in Python") else: print(f"{value} is falsy in Python")

python3 truthy.py

0 is falsy in Python

A Ruby method that returns a count and then checks it with if count works for zero; the same code in Python skips the branch. When porting, write the comparison out (count > 0) in both languages and the question disappears.

What differs

Neither list says one language is better. Ruby keeps a lot in the expression; Python keeps a lot in the statement. Both are consistent, and a reader who knows one can predict most of the other after a day.

Questions

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

  1. Which values are falsy in Ruby?

    Answer

    Only false and nil

    Everything else, zero and the empty string included, is true in a condition; Python has a longer list of falsy values.

  2. What does this program print? value = 0 puts "#{value} is truthy in Ruby" if value puts "done"

    Answer

    0 is truthy in Ruby / done

    Zero is truthy in Ruby, so the guarded line runs, and the second puts runs either way.

  3. What does this program print? p (1..5).select(&:even?).map { |n| n * n }.sum

    Answer

    20

    The even numbers in the range are 2 and 4, and the sum of their squares is twenty.

  4. What does this program print? name = "Ada" puts "Hello, #{name}"

    Answer

    Hello, Ada

    The double quotes interpolate the variable, which is the Ruby spelling of the Python f-string.

  5. What does Python read that Ruby ignores?

    Answer

    Indentation, which is part of the syntax

    A Ruby file with four spaces instead of two still runs, while a Python block is defined by its indentation.

  6. What does this program print? p [nil, false, 0].count { |v| v }

    Answer

    1

    Only zero is truthy, so the block counts one element while Python would count none of the three.

  7. What does this program print? p [1, 2, 3].select(&:even?)

    Answer

    [2]

    The symbol-to-proc form is the short block Ruby sends to select, and one element is even.

  8. Which Python form interpolates a value into a string?

    Answer

    a string prefixed with f, as in f"Hello, {name}"

    Ruby writes the value inside a double-quoted string with #{}, and Python needs the f prefix.

  9. What does this program print? p [].empty? p [].any?

    Answer

    true / false

    An empty array is empty and has no truthy element, which is how the two languages test a list.

  10. A Python program writes if count: and Ruby writes if count. When do they disagree?

    Answer

    When count is zero: Ruby takes the branch, Python does not

    The safe port is an explicit comparison such as count > 0, which reads the same in both languages.

Article author: Arthur Isaev