[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

"case when" executes a symbol as method

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



5 Answers

Devin Mullins

6/14/2007 1:22:00 PM

0

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

dblack

6/14/2007 2:16:00 PM

0

Francisco Laguna

6/14/2007 9:49:00 PM

0

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 dblack@wobblini.net:

> Hi --
>
> On Thu, 14 Jun 2007, Francisco Laguna wrote:
>
>> 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/978...)
> RUBY FOR RAILS (http://www.manning...)
> * Ruby/Rails training
> & consulting: Ruby Power and Light, LLC (http://www.r...)
>


Eric Hodel

6/17/2007 1:28:00 AM

0

On Jun 14, 2007, at 07:16, dblack@wobblini.net 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).

--
Poor workers blame their tools. Good workers build better tools. The
best
workers get their tools to do the work for them. -- Syndicate Wars



Nobuyoshi Nakada

6/17/2007 8:21:00 AM

0

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.

--
Nobu Nakada