iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💎

Ruby 3.2 - Assignment Evaluation Order and Pattern Matching

に公開

This is the article for day 3 of the Ruby 3.2 Advent Calendar.

https://qiita.com/advent-calendar/2022/ruby32


Evaluation Order of Assignment Expressions

Bug #15928: Constant declaration does not conform to JIS 3017:2013 - Ruby master - Ruby Issue Tracking System

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