# Case statements - Just beautification

**URL:** <https://rubytalk.org/t/case-statements-just-beautification/61427>\
**Category:** ruby-talk\
**Created:** [12 January 2011 11:45 UTC](https://rubytalk.org/t/case-statements-just-beautification/61427 "2011-01-12T11:45:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)\
**Post date:** [12 January 2011 11:45 UTC](https://rubytalk.org/t/case-statements-just-beautification/61427/1 "2011-01-12T11:45:51Z")

</div>

I just want to clarify case statements the name after the word case is  
this just for beautification and to help with code readability? What  
purpose does it serve.

So in my code its "winunit" and matz's code its "birthyear".

# Method 1 - Confident - assign range  
# number -\> number  
def pricerange(x)  
puts "Enter the quoted price: "  
x = gets.chomp  
winalloc = case winunit  
&nbsp;&nbsp;when x == (1.50...2.50) then ...  
&nbsp;&nbsp;when x == (2.50...3.80) then ...  
&nbsp;&nbsp;when x == (3.80...5.50) then ...  
&nbsp;&nbsp;else something  
end

# Case expression tests ranges with ===  
generation = case birthyear  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 1946..1963: "Baby Boomer"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 1964..1976: "Generation X"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 1978..2000: "Generation Y"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

Sayth

---

<div class="post-metadata">

**Author:** ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)\
**Post date:** [12 January 2011 11:50 UTC](https://rubytalk.org/t/case-statements-just-beautification/61427/2 "2011-01-12T11:50:23Z")

</div>

Oops edited mine to look more like Matz's

def pricerange(x)  
puts "Enter the quoted price: "  
x = gets.chomp  
winalloc = case winunit  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x == 1.50...2.50: ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x == 2.50...3.80: ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x == 3.80...5.50: ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else something  
end

> **···**
>
> On Jan 12, 10:43 pm, flebber \<flebber.c...@gmail.com\> wrote:
> 
> > I just want to clarify case statements the name after the word case is  
> > this just for beautification and to help with code readability? What  
> > purpose does it serve.
> > 
> > So in my code its "winunit" and matz's code its "birthyear".
> > 
> > # Method 1 - Confident - assign range  
> > # number -\> number  
> > def pricerange(x)  
> > puts "Enter the quoted price: "  
> > x = gets.chomp  
> > winalloc = case winunit  
> > when x == (1.50...2.50) then ...  
> > when x == (2.50...3.80) then ...  
> > when x == (3.80...5.50) then ...  
> > else something  
> > end
> > 
> > # Case expression tests ranges with ===  
> > generation = case birthyear  
> > when 1946..1963: "Baby Boomer"  
> > when 1964..1976: "Generation X"  
> > when 1978..2000: "Generation Y"  
> > else nil  
> > end
> > 
> > Sayth

---

<div class="post-metadata">

**Author:** ![Tim\_Roberts](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@Tim\_Roberts](https://rubytalk.org/u/Tim_Roberts)\
**Post date:** [13 January 2011 07:45 UTC](https://rubytalk.org/t/case-statements-just-beautification/61427/3 "2011-01-13T07:45:25Z")

</div>

Perhaps its purpose would be clearer if you wrote it like Matz's code:

x = gets.chomp  
winalloc = case x  
&nbsp;&nbsp;when (1.50...2.50) then ...  
&nbsp;&nbsp;when (2.50...3.80) then ...  
&nbsp;&nbsp;when (3.80...5.50) then ...  
&nbsp;&nbsp;else something  
end

> **···**
>
> flebber \<flebber.crue@gmail.com\> wrote:
> 
> > I just want to clarify case statements the name after the word case is  
> > this just for beautification and to help with code readability? What  
> > purpose does it serve.
> > 
> > So in my code its "winunit" and matz's code its "birthyear".
> > 
> > # Method 1 - Confident - assign range  
> > # number -\> number  
> > def pricerange(x)  
> > puts "Enter the quoted price: "  
> > x = gets.chomp  
> > winalloc = case winunit  
> > when x == (1.50...2.50) then ...  
> > when x == (2.50...3.80) then ...  
> > when x == (3.80...5.50) then ...  
> > else something  
> > end
> 
> --  
> Tim Roberts, timr@probo.com  
> Providenza & Boekelheide, Inc.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [12 January 2011 13:21 UTC](https://rubytalk.org/t/case-statements-just-beautification/61427/4 "2011-01-12T13:21:37Z")

</div>

This won't work they way you probably expect. It will evaluate "x ==  
..." expressions to a boolean value (true or false) and then evaluate  
true\>false === winunit which will only ever be true if winunit itself  
is true or false.

You need to decide which of the two forms of case you want to use

1. with a single value

case x  
when A  
when B  
when C  
else  
end

In this case A|B|C === x is evaluated and if it returns a trueish  
value the corresponding branch will be evaluated.

2. without value

case  
when D \> x  
when E.include? x  
when its\_too\_late()  
else  
end

In this case each expression after "when" is evaluated and if it  
returns a trueish value the corresponding branch will be evaluated.

Note: trueish values are all values except nil and false.

Kind regards

robert

> **···**
>
> On Wed, Jan 12, 2011 at 12:50 PM, flebber \<flebber.crue@gmail.com\> wrote:
> 
> > On Jan 12, 10:43 pm, flebber \<flebber.c...@gmail.com\> wrote:
> > 
> > > I just want to clarify case statements the name after the word case is  
> > > this just for beautification and to help with code readability? What  
> > > purpose does it serve.
> > > 
> > > So in my code its "winunit" and matz's code its "birthyear".
> > > 
> > > # Method 1 - Confident - assign range  
> > > # number -\> number  
> > > def pricerange(x)  
> > > puts "Enter the quoted price: "  
> > > x = gets.chomp  
> > > winalloc = case winunit  
> > > when x == (1.50...2.50) then ...  
> > > when x == (2.50...3.80) then ...  
> > > when x == (3.80...5.50) then ...  
> > > else something  
> > > end
> > > 
> > > # Case expression tests ranges with ===  
> > > generation = case birthyear  
> > > when 1946..1963: "Baby Boomer"  
> > > when 1964..1976: "Generation X"  
> > > when 1978..2000: "Generation Y"  
> > > else nil  
> > > end
> > > 
> > > Sayth
> > 
> > Oops edited mine to look more like Matz's
> > 
> > def pricerange(x)  
> > puts "Enter the quoted price: "  
> > x = gets.chomp  
> > winalloc = case winunit  
> > when x == 1.50...2.50: ...  
> > when x == 2.50...3.80: ...  
> > when x == 3.80...5.50: ...  
> > else something  
> > end
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)\
**Post date:** [13 January 2011 10:35 UTC](https://rubytalk.org/t/case-statements-just-beautification/61427/5 "2011-01-13T10:35:20Z")

</div>

I tough my code looked like Matz's

Matz's code  
# Case expression tests ranges with ===  
generation = case birthyear  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 1946..1963: "Baby Boomer"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 1964..1976: "Generation X"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 1978..2000: "Generation Y"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
My Code - (...) are placeholders  
x = gets.chomp  
winalloc = case winunit  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x == 1.50...2.50: ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x == 2.50...3.80: ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x == 3.80...5.50: ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else something

> **···**
>
> On Jan 13, 6:41 pm, Tim Roberts \<t...@probo.com\> wrote:
> 
> > flebber \<flebber.c...@gmail.com\> wrote:  
> > \>I just want to clarify case statements the name after the word case is  
> > \>this just for beautification and to help with code readability? What  
> > \>purpose does it serve.
> > 
> > \>So in my code its "winunit" and matz's code its "birthyear".
> > 
> > \># Method 1 - Confident - assign range  
> > \># number -\> number  
> > \>def pricerange(x)  
> > \>puts "Enter the quoted price: "  
> > \>x = gets.chomp  
> > \>winalloc = case winunit  
> > \> when x == (1.50...2.50) then ...  
> > \> when x == (2.50...3.80) then ...  
> > \> when x == (3.80...5.50) then ...  
> > \> else something  
> > \>end
> > 
> > Perhaps its purpose would be clearer if you wrote it like Matz's code:
> > 
> > x = gets.chomp  
> > winalloc = case x  
> > when (1.50...2.50) then ...  
> > when (2.50...3.80) then ...  
> > when (3.80...5.50) then ...  
> > else something  
> > end  
> > --  
> > Tim Roberts, t...@probo.com  
> > Providenza & Boekelheide, Inc.

---

<div class="post-metadata">

**Author:** ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)\
**Post date:** [13 January 2011 10:35 UTC](https://rubytalk.org/t/case-statements-just-beautification/61427/6 "2011-01-13T10:35:22Z")

</div>

I tough my code looked like Matz's

Matz's code  
# Case expression tests ranges with ===  
generation = case birthyear  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 1946..1963: "Baby Boomer"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 1964..1976: "Generation X"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 1978..2000: "Generation Y"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
My Code - (...) are placeholders  
x = gets.chomp  
winalloc = case winunit  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x == 1.50...2.50: ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x == 2.50...3.80: ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x == 3.80...5.50: ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else something

> **···**
>
> On Jan 13, 6:41 pm, Tim Roberts \<t...@probo.com\> wrote:
> 
> > flebber \<flebber.c...@gmail.com\> wrote:  
> > \>I just want to clarify case statements the name after the word case is  
> > \>this just for beautification and to help with code readability? What  
> > \>purpose does it serve.
> > 
> > \>So in my code its "winunit" and matz's code its "birthyear".
> > 
> > \># Method 1 - Confident - assign range  
> > \># number -\> number  
> > \>def pricerange(x)  
> > \>puts "Enter the quoted price: "  
> > \>x = gets.chomp  
> > \>winalloc = case winunit  
> > \> when x == (1.50...2.50) then ...  
> > \> when x == (2.50...3.80) then ...  
> > \> when x == (3.80...5.50) then ...  
> > \> else something  
> > \>end
> > 
> > Perhaps its purpose would be clearer if you wrote it like Matz's code:
> > 
> > x = gets.chomp  
> > winalloc = case x  
> > when (1.50...2.50) then ...  
> > when (2.50...3.80) then ...  
> > when (3.80...5.50) then ...  
> > else something  
> > end  
> > --  
> > Tim Roberts, t...@probo.com  
> > Providenza & Boekelheide, Inc.

---

<div class="post-metadata">

**Author:** ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)\
**Post date:** [13 January 2011 10:35 UTC](https://rubytalk.org/t/case-statements-just-beautification/61427/7 "2011-01-13T10:35:42Z")

</div>

I tough my code looked like Matz's

Matz's code  
# Case expression tests ranges with ===  
generation = case birthyear  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 1946..1963: "Baby Boomer"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 1964..1976: "Generation X"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 1978..2000: "Generation Y"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
My Code - (...) are placeholders  
x = gets.chomp  
winalloc = case winunit  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x == 1.50...2.50: ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x == 2.50...3.80: ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x == 3.80...5.50: ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else something

> **···**
>
> On Jan 13, 6:41 pm, Tim Roberts \<t...@probo.com\> wrote:
> 
> > flebber \<flebber.c...@gmail.com\> wrote:  
> > \>I just want to clarify case statements the name after the word case is  
> > \>this just for beautification and to help with code readability? What  
> > \>purpose does it serve.
> > 
> > \>So in my code its "winunit" and matz's code its "birthyear".
> > 
> > \># Method 1 - Confident - assign range  
> > \># number -\> number  
> > \>def pricerange(x)  
> > \>puts "Enter the quoted price: "  
> > \>x = gets.chomp  
> > \>winalloc = case winunit  
> > \> when x == (1.50...2.50) then ...  
> > \> when x == (2.50...3.80) then ...  
> > \> when x == (3.80...5.50) then ...  
> > \> else something  
> > \>end
> > 
> > Perhaps its purpose would be clearer if you wrote it like Matz's code:
> > 
> > x = gets.chomp  
> > winalloc = case x  
> > when (1.50...2.50) then ...  
> > when (2.50...3.80) then ...  
> > when (3.80...5.50) then ...  
> > else something  
> > end  
> > --  
> > Tim Roberts, t...@probo.com  
> > Providenza & Boekelheide, Inc.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [13 January 2011 12:06 UTC](https://rubytalk.org/t/case-statements-just-beautification/61427/8 "2011-01-13T12:06:23Z")

</div>

You are not using Range#=== as Matz did. Please look carefully and  
read my previous reply.

Cheers

robert

> **···**
>
> On Thu, Jan 13, 2011 at 11:35 AM, flebber \<flebber.crue@gmail.com\> wrote:
> 
> > On Jan 13, 6:41 pm, Tim Roberts \<t...@probo.com\> wrote:
> > 
> > > flebber \<flebber.c...@gmail.com\> wrote:  
> > > \>I just want to clarify case statements the name after the word case is  
> > > \>this just for beautification and to help with code readability? What  
> > > \>purpose does it serve.
> > > 
> > > \>So in my code its "winunit" and matz's code its "birthyear".
> > > 
> > > \># Method 1 - Confident - assign range  
> > > \># number -\> number  
> > > \>def pricerange(x)  
> > > \>puts "Enter the quoted price: "  
> > > \>x = gets.chomp  
> > > \>winalloc = case winunit  
> > > \> when x == (1.50...2.50) then ...  
> > > \> when x == (2.50...3.80) then ...  
> > > \> when x == (3.80...5.50) then ...  
> > > \> else something  
> > > \>end
> > > 
> > > Perhaps its purpose would be clearer if you wrote it like Matz's code:
> > > 
> > > x = gets.chomp  
> > > winalloc = case x  
> > > when (1.50...2.50) then ...  
> > > when (2.50...3.80) then ...  
> > > when (3.80...5.50) then ...  
> > > else something  
> > > end
> 
> > I tough my code looked like Matz's
> > 
> > Matz's code  
> > # Case expression tests ranges with ===  
> > generation = case birthyear  
> > when 1946..1963: "Baby Boomer"  
> > when 1964..1976: "Generation X"  
> > when 1978..2000: "Generation Y"  
> > else nil  
> > end  
> > My Code - (...) are placeholders  
> > x = gets.chomp  
> > winalloc = case winunit  
> > when x == 1.50...2.50: ...  
> > when x == 2.50...3.80: ...  
> > when x == 3.80...5.50: ...  
> > else something
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [13 January 2011 12:25 UTC](https://rubytalk.org/t/case-statements-just-beautification/61427/9 "2011-01-13T12:25:42Z")

</div>

> > \>I just want to clarify case statements the name after the word case is  
> > \>this just for beautification and to help with code readability? What  
> > \>purpose does it serve.
> > 
> > \>So in my code its "winunit" and matz's code its "birthyear".
> > 
> > \># Method 1 - Confident - assign range  
> > \># number -\> number  
> > \>def pricerange(x)  
> > \>puts "Enter the quoted price: "  
> > \>x = gets.chomp  
> > \>winalloc = case winunit  
> > \> when x == (1.50...2.50) then ...  
> > \> when x == (2.50...3.80) then ...  
> > \> when x == (3.80...5.50) then ...  
> > \> else something  
> > \>end
> > 
> > Perhaps its purpose would be clearer if you wrote it like Matz's code:
> > 
> > x = gets.chomp  
> > winalloc = case x  
> > when (1.50...2.50) then ...  
> > when (2.50...3.80) then ...  
> > when (3.80...5.50) then ...  
> > else something  
> > end  
> > --  
> > Tim Roberts, t...@probo.com  
> > Providenza & Boekelheide, Inc.
> 
> I tough my code looked like Matz's

Really? It doesn't look much like it to me. The series of when  
statements following a case statement without an expression work  
pretty much like an if elsif...else sequence. WIth an expression in  
the case statement like

case foo  
when bar  
&nbsp;&nbsp;&nbsp;&nbsp;baz  
when bop  
&nbsp;&nbsp;&nbsp;boo  
else  
&nbsp;&nbsp;&nbsp;shazbot  
end

is equivalent to:  
if bar === foo  
&nbsp;&nbsp;&nbsp;&nbsp;baz  
elsif bop === foo  
&nbsp;&nbsp;&nbsp;boo  
else  
&nbsp;&nbsp;&nbsp;shazbot  
end

Case is kind of like a macro, so I'll talk about case statements  
expanding, but what's really happening is that the Ruby parser and  
interpreter evaluate the code 'as if' it had been written as an  
if;elsif...;else sequence.

Note that the logical expressions in the if and elsif clauses are ===  
messages sent the the when value with the case value as the argument.  
That's why === (with 3 equal signs) is called the case comparison  
operator. Object defines === to be the same as == but certain classes  
redefine it. In particular Range overrides it to be the same as  
Range#include?

> Matz's code  
> # Case expression tests ranges with ===  
> generation = case birthyear  
> when 1946..1963: "Baby Boomer"  
> when 1964..1976: "Generation X"  
> when 1978..2000: "Generation Y"  
> else nil  
> end

This 'expands' to

generation = if (1946..1963) === birthyear  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Baby Boomer"  
&nbsp;&nbsp;&nbsp;elsif (1964..1976) === birthyear  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Generation X"  
&nbsp;&nbsp;&nbsp;elsif 1(978..2000) === birthyear  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Generation Y"  
&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nil  
&nbsp;&nbsp;&nbsp;&nbsp;end

So the value of the case is determined by the first range which  
includes birthyear.

Your code (I'm changing the ...s so we can distinguish the different 'legs'):

> My Code - (...) are placeholders  
> x = gets.chomp  
> winalloc = case winunit  
> when x == 1.50...2.50: something1  
> when x == 2.50...3.80: something2  
> when x == 3.80...5.50: something3  
> else something\_else

'expands' to:

&nbsp;&nbsp;winalloc = if (x == 1.50...2.50) === winunit  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;something1  
&nbsp;&nbsp;&nbsp;elsif (x == 2.50...3.80) === winunit  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;something2  
&nbsp;&nbsp;&nbsp;elsif (x == 3.80...5.50) === winunit  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;something3  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;something\_else  
&nbsp;&nbsp;&nbsp;&nbsp;end

We can see by the code above the case that x is a string (the result  
of gets chomp), so all those x == \<range\> expressions will evaluate  
to false since x is a string and not a Range. So if we substitute:

&nbsp;&nbsp;winalloc = if false === winunit  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;something1  
&nbsp;&nbsp;&nbsp;elsif false === winunit  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sometthing2  
&nbsp;&nbsp;&nbsp;elsif false === winunit  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;something3  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;something\_else  
&nbsp;&nbsp;&nbsp;&nbsp;end

which can be simplified to:

&nbsp;&nbsp;winalloc = if false === winunit  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;something1  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;something\_else  
&nbsp;&nbsp;&nbsp;&nbsp;end

which should be obvious I hope.

Note that false, being the sole instance of FalseClass uses Object#===  
so false === winunit will only be true if the variable winunit  
references the unique false object.

So since we don't know what winunit actually is from the snippet  
you've given, the result will either be something1 iff winunit ==  
false, and something\_else otherwise. If this is the first appearance  
of winunit it will be nil, so the case expression will always return  
something\_else.

Note that since x is a string (the result of gets chomp) it won't be a  
member of any of those ranges. I'm guessing that maybe winunit is  
meant to be the integer value of that string. If so maybe you want  
something like this:

x = gets.chomp  
winunit = x.to\_i  
winalloc = case winunit  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 1.50...2.50: ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 2.50...3.80: ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 3.80...5.50: ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else something

Which should look a lot more like the code in Matz and Flanigan.

HTH

> **···**
>
> On Thu, Jan 13, 2011 at 5:35 AM, flebber \<flebber.crue@gmail.com\> wrote:
> 
> > On Jan 13, 6:41 pm, Tim Roberts \<t...@probo.com\> wrote:
> > 
> > > flebber \<flebber.c...@gmail.com\> wrote:
> 
> --  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Github: [http://github.com/rubyredrick](http://github.com/rubyredrick)  
> Twitter: @RickDeNatale  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)\
**Post date:** [14 January 2011 11:40 UTC](https://rubytalk.org/t/case-statements-just-beautification/61427/10 "2011-01-14T11:40:45Z")

</div>

Thanks for the explanation. Yes that clears up the question.

> **···**
>
> On Jan 13, 7:25 am, Rick DeNatale \<rick.denat...@gmail.com\> wrote:
> 
> > On Thu, Jan 13, 2011 at 5:35 AM, flebber \<flebber.c...@gmail.com\> wrote:  
> > \> On Jan 13, 6:41 pm, Tim Roberts \<t...@probo.com\> wrote:  
> > \>\> flebber \<flebber.c...@gmail.com\> wrote:  
> > \>\> \>I just want to clarify case statements the name after the word case is  
> > \>\> \>this just for beautification and to help with code readability? What  
> > \>\> \>purpose does it serve.
> > 
> > \>\> \>So in my code its "winunit" and matz's code its "birthyear".
> > 
> > \>\> \># Method 1 - Confident - assign range  
> > \>\> \># number -\> number  
> > \>\> \>def pricerange(x)  
> > \>\> \>puts "Enter the quoted price: "  
> > \>\> \>x = gets.chomp  
> > \>\> \>winalloc = case winunit  
> > \>\> \> when x == (1.50...2.50) then ...  
> > \>\> \> when x == (2.50...3.80) then ...  
> > \>\> \> when x == (3.80...5.50) then ...  
> > \>\> \> else something  
> > \>\> \>end
> > 
> > \>\> Perhaps its purpose would be clearer if you wrote it like Matz's code:
> > 
> > \>\> x = gets.chomp  
> > \>\> winalloc = case x  
> > \>\> when (1.50...2.50) then ...  
> > \>\> when (2.50...3.80) then ...  
> > \>\> when (3.80...5.50) then ...  
> > \>\> else something  
> > \>\> end  
> > \>\> --  
> > \>\> Tim Roberts, t...@probo.com  
> > \>\> Providenza & Boekelheide, Inc.
> > 
> > \> I tough my code looked like Matz's
> > 
> > Really? It doesn't look much like it to me. The series of when  
> > statements following a case statement without an expression work  
> > pretty much like an if elsif...else sequence. WIth an expression in  
> > the case statement like
> > 
> > case foo  
> > when bar  
> > baz  
> > when bop  
> > boo  
> > else  
> > shazbot  
> > end
> > 
> > is equivalent to:  
> > if bar === foo  
> > baz  
> > elsif bop === foo  
> > boo  
> > else  
> > shazbot  
> > end
> > 
> > Case is kind of like a macro, so I'll talk about case statements  
> > expanding, but what's really happening is that the Ruby parser and  
> > interpreter evaluate the code 'as if' it had been written as an  
> > if;elsif...;else sequence.
> > 
> > Note that the logical expressions in the if and elsif clauses are ===  
> > messages sent the the when value with the case value as the argument.  
> > That's why === (with 3 equal signs) is called the case comparison  
> > operator. Object defines === to be the same as == but certain classes  
> > redefine it. In particular Range overrides it to be the same as  
> > Range#include?
> > 
> > \> Matz's code  
> > \> # Case expression tests ranges with ===  
> > \> generation = case birthyear  
> > \> when 1946..1963: "Baby Boomer"  
> > \> when 1964..1976: "Generation X"  
> > \> when 1978..2000: "Generation Y"  
> > \> else nil  
> > \> end
> > 
> > This 'expands' to
> > 
> > generation = if (1946..1963) === birthyear  
> > "Baby Boomer"  
> > elsif (1964..1976) === birthyear  
> > "Generation X"  
> > elsif 1(978..2000) === birthyear  
> > "Generation Y"  
> > else  
> > nil  
> > end
> > 
> > So the value of the case is determined by the first range which  
> > includes birthyear.
> > 
> > Your code (I'm changing the ...s so we can distinguish the different 'legs'):
> > 
> > \> My Code - (...) are placeholders  
> > \> x = gets.chomp  
> > \> winalloc = case winunit  
> > \> when x == 1.50...2.50: something1  
> > \> when x == 2.50...3.80: something2  
> > \> when x == 3.80...5.50: something3  
> > \> else something\_else
> > 
> > 'expands' to:
> > 
> > winalloc = if (x == 1.50...2.50) === winunit  
> > something1  
> > elsif (x == 2.50...3.80) === winunit  
> > something2  
> > elsif (x == 3.80...5.50) === winunit  
> > something3  
> > else  
> > something\_else  
> > end
> > 
> > We can see by the code above the case that x is a string (the result  
> > of gets chomp), so all those x == \<range\> expressions will evaluate  
> > to false since x is a string and not a Range. So if we substitute:
> > 
> > winalloc = if false === winunit  
> > something1  
> > elsif false === winunit  
> > sometthing2  
> > elsif false === winunit  
> > something3  
> > else  
> > something\_else  
> > end
> > 
> > which can be simplified to:
> > 
> > winalloc = if false === winunit  
> > something1  
> > else  
> > something\_else  
> > end
> > 
> > which should be obvious I hope.
> > 
> > Note that false, being the sole instance of FalseClass uses Object#===  
> > so false === winunit will only be true if the variable winunit  
> > references the unique false object.
> > 
> > So since we don't know what winunit actually is from the snippet  
> > you've given, the result will either be something1 iff winunit ==  
> > false, and something\_else otherwise. If this is the first appearance  
> > of winunit it will be nil, so the case expression will always return  
> > something\_else.
> > 
> > Note that since x is a string (the result of gets chomp) it won't be a  
> > member of any of those ranges. I'm guessing that maybe winunit is  
> > meant to be the integer value of that string. If so maybe you want  
> > something like this:
> > 
> > x = gets.chomp  
> > winunit = x.to\_i  
> > winalloc = case winunit  
> > when 1.50...2.50: ...  
> > when 2.50...3.80: ...  
> > when 3.80...5.50: ...  
> > else something
> > 
> > Which should look a lot more like the code in Matz and Flanigan.
> > 
> > HTH  
> > --  
> > Rick DeNatale
> > 
> > Blog:[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> > Github:[rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
> > Twitter: @RickDeNatale  
> > WWR:[http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> > LinkedIn:[http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
