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.
-
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.
-
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.
-
What does this program print?
case [1, 2] in [a, b] puts a + b endAnswer
3
The pattern binds the two elements, and the branch adds them.
-
What does this program print?
user = { name: "Ada", password: "secret" } puts user.except(:password).inspectAnswer
{name: "Ada"}
except returns a new hash without the named key, and inspect prints the symbol keys in the short form.
-
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.
-
What does
_1stand 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.
-
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.
-
What does
case-inmatch thatcase-whendoes 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.
-
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.
-
What is the advantage of a library method such as
exceptover 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