irb(main):138:0> case 1
irb(main):139:1> when 1..10 then 'in'
irb(main):140:1> else 'not in'
irb(main):141:1> end
obviously, the result is 'in'
In the docs, there are an explanation:
*case operates by comparing the target (the expression after the keyword
case) with each of the comparison expressions after the when keywords. This
test is done using comparison === target. As long as a class defines
meaningful semantics for === (and all the built-in classes do), objects of
that class can be used in case expressions. *
But, when I type this, why throw an error ?
irb(main):149:0> 1..10 === 1
ArgumentError: bad value for range
from (irb):149
from /opt/ruby-1.9.3/bin/irb:12:in `<main>'
irb(main):138:0> case 1
irb(main):139:1> when 1..10 then 'in'
irb(main):140:1> else 'not in'
irb(main):141:1> end
obviously, the result is 'in'
In the docs, there are an explanation:
*case operates by comparing the target (the expression after the keyword
case) with each of the comparison expressions after the when keywords.
This test is done using comparison === target. As long as a class defines
meaningful semantics for === (and all the built-in classes do), objects
of that class can be used in case expressions. *
But, when I type this, why throw an error ?
irb(main):149:0> 1..10 === 1
ArgumentError: bad value for range
from (irb):149
from /opt/ruby-1.9.3/bin/irb:12:in `<main>'
irb(main):138:0> case 1
irb(main):139:1> when 1..10 then 'in'
irb(main):140:1> else 'not in'
irb(main):141:1> end
obviously, the result is 'in'
In the docs, there are an explanation:
*case operates by comparing the target (the expression after the keyword
case) with each of the comparison expressions after the when keywords. This
test is done using comparison === target. As long as a class defines
meaningful semantics for === (and all the built-in classes do), objects of
that class can be used in case expressions. *
But, when I type this, why throw an error ?
irb(main):149:0> 1..10 === 1
ArgumentError: bad value for range
from (irb):149
from /opt/ruby-1.9.3/bin/irb:12:in `<main>'