"case when" executes a symbol as method

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

Francisco Laguna wrote:

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?

Syntax/whitespace thing. No special meta-magic.

puts case greeting
when “Hello” :hello
when “Good Day” :good_day
end
is
puts case greeting
when “Hello”: hello
when “Good Day”: good_day
end
is
puts case greeting
when “Hello” then hello
when “Good Day” then good_day
end
is
puts case greeting
when “Hello”
hello
when “Good Day”
good_day
end

The equivalent of the first example would be:

puts case greeting
when “Hello”: :hello
when “Good Day”: :good_day
end

Hi –

On Thu, 14 Jun 2007, Francisco Laguna wrote:

“Hello World!”
:hello
def good_day

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?

I believe it’s being parsed as:

when “Hello”: hello

Note the : which can also separate the when part from the value.

Don’t be disappointed. It would be beyond bizarre if a symbol
suddenly decided to be a method call because of something like which
line it was written on.

David

On Jun 14, 2007, at 07:16, [email protected] wrote:

I believe it’s being parsed as:

when “Hello”: hello

Note the : which can also separate the when part from the value.

Don’t be disappointed. It would be beyond bizarre if a symbol
suddenly decided to be a method call because of something like which
line it was written on.

Oh good, now I have a real reason to hate ‘when blah:’ (besides it
making me feel weird).

Hi,

At Sun, 17 Jun 2007 10:27:44 +0900,
Eric H. wrote in [ruby-talk:255921]:

Oh good, now I have a real reason to hate ‘when blah:’ (besides it
making me feel weird).

Don’t mind, it is deplicated in 1.9.

Thanks David and Devin for pointing that out! Looks like the syntax
highlighting in my editor tricked my head into parsing that weirdly :smiley:

Am 14.06.2007 um 16:16 schrieb [email protected]:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs