Case statements - Just beautification

F

flebber

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
 
F

flebber

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
 
R

Robert Klemme

Oops edited mine to look more like Matz's

def pricerange(x)
puts "Enter the quoted price: "
x =3D gets.chomp
winalloc =3D case winunit
=A0 =A0 =A0 =A0 =A0when x =3D=3D 1.50...2.50: ...
=A0 =A0 =A0 =A0 =A0when x =3D=3D 2.50...3.80: ...
=A0 =A0 =A0 =A0 =A0when x =3D=3D 3.80...5.50: ...
=A0 =A0 =A0 =A0 =A0else something
end

This won't work they way you probably expect. It will evaluate "x =3D=3D
..." expressions to a boolean value (true or false) and then evaluate
true|false =3D=3D=3D 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 =3D=3D=3D 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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
F

flebber

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
 
F

flebber

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
 
F

flebber

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
 
R

Robert Klemme

I tough my code looked like Matz's

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

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

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Rick DeNatale

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 =3D=3D=3D foo
baz
elsif bop =3D=3D=3D 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 =3D=3D=3D
messages sent the the when value with the case value as the argument.
That's why =3D=3D=3D (with 3 equal signs) is called the case comparison
operator. Object defines =3D=3D=3D to be the same as =3D=3D but certain cl=
asses
redefine it. In particular Range overrides it to be the same as
Range#include?
Matz's code
# Case expression tests ranges with =3D=3D=3D
generation =3D case birthyear
=A0 =A0 =A0 =A0 =A0 =A0 when 1946..1963: "Baby Boomer"
=A0 =A0 =A0 =A0 =A0 =A0 when 1964..1976: "Generation X"
=A0 =A0 =A0 =A0 =A0 =A0 when 1978..2000: "Generation Y"
=A0 =A0 =A0 =A0 =A0 =A0 else nil
=A0 =A0 =A0 =A0 =A0 =A0 end

This 'expands' to

generation =3D if (1946..1963) =3D=3D=3D birthyear
"Baby Boomer"
elsif (1964..1976) =3D=3D=3D birthyear
"Generation X"
elsif 1(978..2000) =3D=3D=3D 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 =3D gets.chomp
winalloc =3D case winunit
=A0 =A0 =A0 =A0 =A0when x =3D=3D 1.50...2.50: something1
=A0 =A0 =A0 =A0 =A0when x =3D=3D 2.50...3.80: something2
=A0 =A0 =A0 =A0 =A0when x =3D=3D 3.80...5.50: something3
=A0 =A0 =A0 =A0 =A0else something_else

'expands' to:

winalloc =3D if (x =3D=3D 1.50...2.50) =3D=3D=3D winunit
something1
elsif (x =3D=3D 2.50...3.80) =3D=3D=3D winunit
something2
elsif (x =3D=3D 3.80...5.50) =3D=3D=3D 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 =3D=3D <range> expressions will evaluate
to false since x is a string and not a Range. So if we substitute:

winalloc =3D if false =3D=3D=3D winunit
something1
elsif false =3D=3D=3D winunit
sometthing2
elsif false =3D=3D=3D winunit
something3
else
something_else
end

which can be simplified to:

winalloc =3D if false =3D=3D=3D winunit
something1
else
something_else
end

which should be obvious I hope.

Note that false, being the sole instance of FalseClass uses Object#=3D=3D=
=3D
so false =3D=3D=3D 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 =3D=3D
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 =3D gets.chomp
winunit =3D x.to_i
winalloc =3D 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
--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
F

flebber

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?


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'):


'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/
Github:http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR:http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn:http://www.linkedin.com/in/rickdenatale

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

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top