Issue #10064 has been updated by Tsuyoshi Sawada.


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

----------------------------------------
Feature #10064: &:symbol in when clause
https://bugs.ruby-lang.org/issues/10064#change-49153

* Author: Kenta Murata
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* Category: syntax
* Target version: current: 2.2.0
----------------------------------------
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?




-- 
https://bugs.ruby-lang.org/