"case when" executes a symbol as method

F

Francisco Laguna

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
 
D

Devin Mullins

Francisco said:
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:
 
D

dblack

Hi --

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?

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

--
* Books:
RAILS ROUTING (new! http://safari.awprofessional.com/9780321509246)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
F

Francisco Laguna

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

Am 14.06.2007 um 16:16 schrieb (e-mail address removed):
 
E

Eric Hodel

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).
 
N

Nobuyoshi Nakada

Hi,

At Sun, 17 Jun 2007 10:27:44 +0900,
Eric Hodel 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top