From: 6ftdan@... Date: 2015-11-25T18:56:44+00:00 Subject: [ruby-core:71686] [Ruby trunk - Feature #11737] Pass in expression to then block in `case expression` Issue #11737 has been updated by Daniel P. Clark. Since https://bugs.ruby-lang.org/issues/11734#note-3 has informed me the `_` is an IRB feature and Minitest uses `_()` in its test suite perhaps we should not use `_` for the case/when/then. How about `__case__` ~~~ruby case Enumerator.new do |y| y << 1; y << 2; y << 3; end when ->e{ 2.times{e.next}; true} then __case__.peek end == 3 ~~~ ---------------------------------------- Feature #11737: Pass in expression to then block in `case expression` https://bugs.ruby-lang.org/issues/11737#change-55090 * Author: Daniel P. Clark * Status: Open * Priority: Normal * Assignee: ---------------------------------------- Ruby's `case ` scenario allows the expression to be tested in the `when` test with `:===`, but in the then block the expression is not accessible. Only if the expression has been assigned to a variable beforehand can it be checked. ~~~ruby case 4 when ->i{ puts :when; true} ->i{ puts i} else :foo end # when # => # case 4 when ->i{ puts :when; true} puts _ else :foo end # when # # # => nil case 4 when 4 then _ end # => nil case 4 when 4 then ->i{puts i} end # => # ~~~ If some one wanted to give an expression after case that wasn't assigned to a variable then there is no access to it in the then block. I suggest assigning the expression to the `_` variable during a case/when/then scenario. Here's a rather contrived example use case. ~~~ruby case Enumerator.new do |y| y << 1; y << 2; y << 3; end when ->e{ 2.times e.next; true} then _.peek end == 3 ~~~ -- https://bugs.ruby-lang.org/