Hi, could you explain me why this works? It’s a special behaviour of the
“case” statment?
“case” uses operator “===” to check conditions. If you do “case x
when y” then “y === x” is invoked to check the condition (note the
order of x and y). You can even cook your own
positive = lambda {|x| x >= 0}
class << positive
alias :=== :call
end
(-4…4).each do |i|
case i
when positive
puts “#{i} pos”
else
puts “#{i} neg”
end
end
btw, there is also another form of case
case # no value here!
when i >= 0
puts “#{i} pos”
else
puts “#{i} neg”
end
Kind regards
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.