Robert Klemme
6/5/2008 6:39:00 PM
On 05.06.2008 19:26, Junkone wrote:
> irb(main):003:0> case a
> irb(main):004:1> when 0
> irb(main):005:1> puts "0"
> irb(main):006:1> when >0
> irb(main):007:1> puts "greater"
> irb(main):008:1> end
> SyntaxError: compile error
> (irb):6: syntax error, unexpected '>'
> when >0
> ^
> from (irb):8
You can:
$ ruby -ce 'case;when a == 0;puts "0";when a > 0;puts "greater";end'
Syntax OK
This is the second form of "case".
Btw, there is also another way: define a criteria that implements === as
>0:
irb(main):001:0> POS = Object.new
=> #<Object:0x7ff9e244>
irb(main):002:0> def POS.===(x) x > 0 end
=> nil
irb(main):003:0> a=10
=> 10
irb(main):004:0> case a
irb(main):005:1> when 0
irb(main):006:1> puts "0"
irb(main):007:1> when POS
irb(main):008:1> puts "positive"
irb(main):009:1> end
positive
=> nil
irb(main):010:0>
Kind regards
robert