You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
case/when skips all but the last statement of a parenthesized when value #10049
When a when value is a group of statements in parentheses, when (a; b), JRuby evaluates only the last statement and skips the others. CRuby evaluates all of them, in order, and tests the value of the last one. The skipped statements are usually assignments or calls with side effects, so the code silently does less than it says. The tested value can change too: below, x is never assigned, so when (x = 3; x) tests nil and the case does not match.
CRuby 4.0.9 (ruby 4.0.9 (2026-10-23 revision 3e93b23afc) +PRISM [aarch64-linux]), and 3.4.12 prints the same:
[:matched, 1]
[:matched, 3]
[1, 2]
JRuby 10.0.8.0 (jruby 10.0.8.0 (3.4.12) 2026-11-05 ef393d4e54 OpenJDK 64-Bit Server VM 21.0.13+9-LTS on 21.0.13+9-LTS +indy +jit [aarch64-linux]) and 10.1.3.0 (jruby 10.1.3.0 (4.0.9) 2026-11-05 3db52d02f9 OpenJDK 64-Bit Server VM 21.0.13+9-LTS on 21.0.13+9-LTS +indy +jit [aarch64-linux]):
[:matched, 0]
[nil, nil]
[]
A build of master (1c84618) prints the same. With -Xparser.prism on 10.1.3.0 it prints the same as CRuby. A case without a subject (case; when (log << 1; true)) skips the statements too. A single expression in parentheses, like when (foo), is evaluated.
When the when value is a BlockNode, IRBuilderAST#buildWhenArgs builds only list.getLast() (IRBuilderAST.java#L1109-L1110). The branch was added in b79cce9 ("parser passes block node vs only single last value to when"). Before it, each statement was tested as a separate when value. Building the whole BlockNode as the when value runs every statement and tests only the value of the last one, which is what CRuby does.
ruby/spec's "accepts complex expressions within ()" uses when (raise if 2+2 == 3; /a/), where the skipped statement does nothing, so the specs do not catch this (case_spec.rb#L422-L427).
Found while comparing JRuby with CRuby on ruby/spec-based checks.
Activity