I remember something about procs being allowed in case
statements in Ruby 2.0, but I can't google it.
I tried checking Ruby 2.0.0 NEWS and How to write a switch statement in Ruby. I also visited http://ruby-doc.org , but the link it had for keywords was for Ruby 1.9, not Ruby 2.0.
Are procs allowed in case statements?
Yes.
However, this is no different than 1.9.1 since
Proc#===
was defined back then. Since ruby-docs seems to have a problem showing this method, to be clear the documentation says thatproc === obj
:For the Ruby beginner, then
when
clause in Ruby'scase
statements takes the value in the clause and calls the===
method on it, passing in the argument to the case statement. So, for example, this code……runs
/^cat/ === "cats"
to decide if it's a match; theRegExp
class defines the===
method to perform regex matching. Thus, you can use your own object in awhen
clause as long as you define===
for it.