iTranslated by AI
Ruby 3.2 - Assignment Evaluation Order and Pattern Matching
This is the article for day 3 of the Ruby 3.2 Advent Calendar.
Evaluation Order of Assignment Expressions
When setting a constant, the evaluation order of the object where the constant is defined and the expression being assigned has changed.
def foo
p :foo
Object
end
def bar
p :bar
end
foo::Hoge = bar
Running the script above results in the following output.
Ruby 3.1:
:bar
:foo
The right-hand side is evaluated first. This has changed in 3.2,
Ruby 3.2:
:foo
:bar
It seems that in assignment expressions, the left-hand side is now always evaluated first.
Pattern Matching
Feature #18585: Promote find pattern to official feature - Ruby master - Ruby Issue Tracking System
The "Find pattern" in pattern matching has become an official feature.
a = ['a', 1, 'b', 2, 'c', 3]
a in [*, Integer => x, String => y, *]
p [x, y] #=> [1, "b"]
In Ruby 3.1, a warning like the following was displayed, but it no longer appears in 3.2.
warning: Find pattern is experimental, and the behavior may change in future versions of Ruby!
I guess it means you can specify arbitrary objects with *. I don't really understand pattern matching at all.
Discussion