# "case when" executes a symbol as method

**URL:** <https://www.ruby-forum.com/t/case-when-executes-a-symbol-as-method/106839>\
**Category:** Ruby\
**Created:** [June 14, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/case-when-executes-a-symbol-as-method/106839 "2007-06-14T15:07:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francisco\_Laguna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a88e4f/32.png) [@Francisco\_Laguna](https://www.ruby-forum.com/u/Francisco_Laguna)\
**Post date:** [June 14, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/case-when-executes-a-symbol-as-method/106839/1 "2007-06-14T15:07:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Francisco\_Laguna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a88e4f/32.png) [@Francisco\_Laguna](https://www.ruby-forum.com/u/Francisco_Laguna)\
**Post date:** [June 14, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/case-when-executes-a-symbol-as-method/106839/2 "2007-06-14T15:22:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Francisco\_Laguna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a88e4f/32.png) [@Francisco\_Laguna](https://www.ruby-forum.com/u/Francisco_Laguna)\
**Post date:** [June 14, 2007, 4:18pm UTC](https://www.ruby-forum.com/t/case-when-executes-a-symbol-as-method/106839/3 "2007-06-14T16:18:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Francisco\_Laguna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a88e4f/32.png) [@Francisco\_Laguna](https://www.ruby-forum.com/u/Francisco_Laguna)\
**Post date:** [June 17, 2007, 3:28am UTC](https://www.ruby-forum.com/t/case-when-executes-a-symbol-as-method/106839/4 "2007-06-17T03:28:17Z")

</div>

On Jun 14, 2007, at 07:16, removed\_email\_address@domain.invalid 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).

---

<div class="post-metadata">

**Author:** ![Francisco\_Laguna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a88e4f/32.png) [@Francisco\_Laguna](https://www.ruby-forum.com/u/Francisco_Laguna)\
**Post date:** [June 17, 2007, 10:21am UTC](https://www.ruby-forum.com/t/case-when-executes-a-symbol-as-method/106839/5 "2007-06-17T10:21:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Francisco\_Laguna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a88e4f/32.png) [@Francisco\_Laguna](https://www.ruby-forum.com/u/Francisco_Laguna)\
**Post date:** [June 14, 2007, 11:50pm UTC](https://www.ruby-forum.com/t/case-when-executes-a-symbol-as-method/106839/6 "2007-06-14T23:50:21Z")

</div>

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

Am 14.06.2007 um 16:16 schrieb removed\_email\_address@domain.invalid:
