Project

General

Profile

Feature #10064

&:symbol in when clause

Added by mrkn (Kenta Murata) about 6 years ago. Updated over 2 years ago.

Status:
Feedback
Priority:
Normal
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?

Also available in: Atom PDF