## Feature #10064

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

**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?

just idea.

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

**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.

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
```

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
```

**Target version** deleted (*2.2.0*)

Also available in: Atom
PDF

Like0
Like0Like0Like0Like0Like0