Project

General

Profile

Actions

Feature #10064

closed

&:symbol in when clause

Feature #10064: &:symbol in when clause

Added by mrkn (Kenta Murata) over 11 years ago. Updated almost 8 years ago.

Status:
Feedback
Target version:
-
[ruby-core:63835]

Description

Now we can put Proc objects in a when clause as this code.

require 'prime' def test(n) print "#{n} is " case n when :zero?.to_proc puts 'zero' when :even?.to_proc puts 'an even number' when :prime?.to_proc puts 'a prime number' else puts 'a non-prime odd number' end end (1..10).each &method(:test) 

I would like to write it as below:

require 'prime' def test(n) print "#{n} is " case n when &:zero? puts 'zero' when &:even? puts 'an even number' when &:prime? puts 'a prime number' else puts 'a non-prime odd number' end end (1..10).each &method(:test) 

How do you think about this new syntax, Matz?

Updated by ko1 (Koichi Sasada) over 11 years ago Actions #1 [ruby-core:63851]

just idea.

class Symbol def ~@ self.to_proc end end case 0 when ~:zero? p :zero end 

Updated by matz (Yukihiro Matsumoto) over 11 years ago Actions #2 [ruby-core:63867]

  • Status changed from Open to Feedback

The original idea includes dispatch based on type (Symbol), which is not a good idea in general.
The one proposed by ko1 is slightly better, yet I still don't love it.

Matz.

Updated by sawa (Tsuyoshi Sawada) about 11 years ago Actions #3 [ruby-core:65338]

What about allowing syntax like this:

->&Proc.new{|y| y.zero?} #=> Should be equivalent to ->{|x| x.zero?} ->&:zero? #=> Should be equivalent to ->{|x| x.zero?} (Symbol#to_proc applies implicitly) 

This would be a parallel to

[*["foo"]] #=> ["foo"] [*{foo: "bar"}] #=> [[:foo, "bar"]] (Symbol#to_a applies implicitly) 

under the understanding that what [] is to * (converse operation) is what -> is to &.

Then, we would be able to do:

case 0 when ->&:zero? p :zero end 

Updated by marcandre (Marc-Andre Lafortune) about 11 years ago Actions #4 [ruby-core:65342]

Yukihiro Matsumoto wrote:

The original idea includes dispatch based on type (Symbol), which is not a good idea in general.

I'm not sure I understand. I think the original idea would work for other types too; the syntax would just be a shortcut for a call to to_proc.

For example, I'd expect the following to work too:

x = Object.new def x.to_proc -> { true } end case 42 when &x puts "Always true" end 

Updated by naruse (Yui NARUSE) almost 8 years ago Actions #5

  • Target version deleted (2.2.0)
Actions

Also available in: PDF Atom