Francisco Laguna
6/14/2007 1:07:00 PM
Hi List!
I just stumbled upon some interesting behaviour of case when and wanted
to ask a few things about it. It looks like the "case when" construct
executes a method when one leaves a out a newline (or semi-colon, I
guess) after the when differentiation. Consider these to programs:
=== program 1 ===
def hello
"Hello World!"
end
def good_day
"Good Day, World!"
end
greeting = "Hello"
puts case greeting
when "Hello"
:hello
when "Good Day"
:good_day
end
=== program 2 ===
def hello
"Hello World!"
end
def good_day
"Good Day, World!"
end
greeting = "Hello"
puts case greeting
when "Hello" :hello
when "Good Day" :good_day
end
==============
The first just prints out the symbols turned to strings ("hello" or
"good_day", respectively), but the second one acrually executes the
hello and good_day methods and the case block has the return value of
the methods as its own value. Pretty cool, if you ask me. How come? Is
that something I can rely on, or something that might disappear, because
it's just some side-effect?
Thanks for the insight
Cisco