ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: s.wanabe@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:71810] [Ruby trunk - Feature #11737] Pass in expression to then block in `case expression`
Date: Thu, 03 Dec 2015 04:07:58 +0000	[thread overview]
Message-ID: <redmine.journal-55214.20151203040757.d1544e13b4336765@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-11737.20151124205922@ruby-lang.org

Issue #11737 has been updated by _ wanabe.


Nobuyoshi Nakada wrote:
> It reminded me a rejected proposal:
> 
> ~~~ruby
> case expr
> when matcher => result
>   ...
> end
> ~~~

Is the proposal [ruby-dev:17615]?
Or other?

----------------------------------------
Feature #11737: Pass in expression to then block in `case expression`
https://bugs.ruby-lang.org/issues/11737#change-55214

* Author: Daniel P. Clark
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
Ruby's `case <expression>` 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
#  => #<Proc:0x00000000d91e58@(irb):16 (lambda)> 

case 4
when ->i{ puts :when; true}
  puts _
else
  :foo
end
# when
# #<Proc:0x00000000d91e58@(irb):16 (lambda)>
#  => nil 

case 4
when 4
then _
end
#  => nil

case 4
when 4
then ->i{puts i}
end
#  => #<Proc:0x000000015f9be0@(irb):36 (lambda)> 
~~~

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/

  parent reply	other threads:[~2015-12-03  3:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-11737.20151124205922@ruby-lang.org>
2015-11-24 20:59 ` [ruby-core:71664] [Ruby trunk - Feature #11737] [Open] Pass in expression to then block in `case expression` 6ftdan
2015-11-24 21:03 ` [ruby-core:71665] [Ruby trunk - Feature #11737] " 6ftdan
2015-11-25 18:56 ` [ruby-core:71686] " 6ftdan
2015-11-28  0:10 ` [ruby-core:71715] " nobu
2015-11-28  8:56 ` [ruby-core:71719] " 6ftdan
2015-12-03  4:07 ` s.wanabe [this message]
2015-12-04 23:43 ` [ruby-core:71839] " 6ftdan
2015-12-07  7:41 ` [ruby-core:71888] [Ruby trunk - Feature #11737] [Rejected] " matz
2015-12-07 11:22 ` [ruby-core:71902] [Ruby trunk - Feature #11737] " 6ftdan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-55214.20151203040757.d1544e13b4336765@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).