Modern Ruby tour

Endless methods

Short methods fit on one line with the endless form. Write the name, the params, an equals sign, and the single expression body.

Reach for the classic def with end as soon as the logic needs branches, loops, or more than one line.

def hi(name) = "hi #{name}" puts hi("Ada")

ruby endless.rb

hi Ada

Numbered _1 params

Tiny blocks can skip naming params entirely. The _1 placeholder stands for the first block argument automatically.

Numbered params shine in one line iterators and fade fast in longer blocks, where a real name reads better.

["ada", "bob"].each { puts _1 }

ruby numbered.rb

ada bob

Pattern matching recap

Case with in branches matches shape, not just value. An array pattern binds each element to a fresh variable in one move.

This recap stays with the simple array form from the Ruby pattern lesson, which already covers most everyday uses.

case [1, 2] in [a, b] puts a + b end

ruby match.rb

3

Ruby 3.x in production

Modern syntax pays off only on a modern runtime. Pin the Ruby version per project and upgrade through test suites with real coverage.

New library methods like except remove whole manual steps, so prefer them over hand rolled filtering helpers.

user = { name: "Ada", password: "secret" } puts user.except(:password).inspect

ruby except.rb

{name: "Ada"}

Questions

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

  1. What does this program print? def hi(name) = "hi #{name}" puts hi("Ada")

    Answer

    hi Ada

    The endless definition returns its single expression, and puts prints the string without quotes.

  2. What does this program print? ["ada", "bob"].each { puts _1 }

    Answer

    ada / bob

    The numbered parameter _1 is the current element, so each iteration prints one name.

  3. What does this program print? case [1, 2] in [a, b] puts a + b end

    Answer

    3

    The pattern binds the two elements, and the branch adds them.

  4. What does this program print? user = { name: "Ada", password: "secret" } puts user.except(:password).inspect

    Answer

    {name: "Ada"}

    except returns a new hash without the named key, and inspect prints the symbol keys in the short form.

  5. When should an endless method be replaced by a classic def ... end?

    Answer

    As soon as the body needs a branch, a loop or more than one line

    The endless form holds one expression, and the multi-line body is what the classic form is for.

  6. What does _1 stand for in a block?

    Answer

    The first argument of the block

    The numbered parameter is the argument by position, without a name of its own.

  7. Why do numbered params fade in a longer block?

    Answer

    A real name says what the value is, and _1 does not

    The one-line block reads well with a placeholder, and a longer body needs the meaning a name carries.

  8. What does case-in match that case-when does not?

    Answer

    The shape of a value and the parts inside it

    A pattern tests structure and binds the parts at the same time, which a when branch does not do.

  9. Why pin the Ruby version per project?

    Answer

    So the modern syntax the code uses is guaranteed to exist on the runtime

    A feature the code relies on has to be in the runtime that runs it, and the pin is what makes that true.

  10. What is the advantage of a library method such as except over a hand-rolled filter?

    Answer

    It removes a whole manual step and is tested with the language

    The standard method is shorter than the loop a reader would otherwise write and maintain.

Article author: Arthur Isaev