From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id BE36F19E0035 for ; Sat, 5 Dec 2015 08:11:39 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 1507AB5D833 for ; Sat, 5 Dec 2015 08:43:11 +0900 (JST) Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id 329FC18CC7E3 for ; Sat, 5 Dec 2015 08:43:11 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 8F592120493; Sat, 5 Dec 2015 08:43:09 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o2.heroku.sendgrid.net (o2.heroku.sendgrid.net [67.228.50.55]) by neon.ruby-lang.org (Postfix) with ESMTPS id 359BA120491 for ; Sat, 5 Dec 2015 08:43:05 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=ku9EprIx9a/sXaTcUyBjrsUHxj4=; b=aBEkwofPhfYQMKQDpQ o0FBjnaQF7ncq03nHnHjOY03FZfeZ5anfbYRcUH7V4q4mfY6HB52CALtaOTsb7Rt o7FsC4ta+o/T+yoepHNEF4cACcwabepw0CM5emS8lROpUV287wyLvcrx6tgpxlrq 2hjalr0XlTmJHXrrj/XqWZhnU= Received: by filter0831p1mdw1.sendgrid.net with SMTP id filter0831p1mdw1.31715.5662250523 2015-12-04 23:43:01.598300879 +0000 UTC Received: from herokuapp.com (ec2-54-211-65-82.compute-1.amazonaws.com [54.211.65.82]) by ismtpd0002p1iad1.sendgrid.net (SG) with ESMTP id jRg1yIRRRtSXUI2SYM9zqw for ; Fri, 04 Dec 2015 23:43:01.432 +0000 (UTC) Date: Fri, 04 Dec 2015 23:43:01 +0000 From: 6ftdan@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Redmine-MailingListIntegration-Message-Ids: 46529 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11737 X-Redmine-Issue-Author: danielpclark X-Redmine-Sender: danielpclark X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS5u4uA6tdKIBkziHNPbsixaialP0GV6owJbuf h2vSKpZmSUA/iYGB46lyj7VTyFj6+jqH6Vdv0l5070anYySFE76Ih3TPW59MFgY+lE65CoIRLF4Fmb 8ACC07zQW2+1EA+MBaAOZEOGKMNeXqOfGClu X-ML-Name: ruby-core X-Mail-Count: 71839 Subject: [ruby-core:71839] [Ruby trunk - Feature #11737] Pass in expression to then block in `case expression` X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #11737 has been updated by Daniel P. Clark. ~~~ruby case 4 # __case__ = 4 when 4 # 4 === __case__ puts "Success! #{__case__} is what we wanted!" # __case__ not defined! ... where did it go? end ~~~ ---------------------------------------- Feature #11737: Pass in expression to then block in `case expression` https://bugs.ruby-lang.org/issues/11737#change-55243 * 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/