help, my 'case/when' code doesn't work

C

cremes.devlist

I've been using ruby for several months now so imagine my surprise when I wrote something using the 'case' construct and I discovered that I didn't understand how to use it! What's worse is that even after looking through the Pickaxe and some code "in the wild" that uses 'case' I still don't see what I'm doing wrong.

Here's the code in question:

def case_test(obj)
print "testing via case... "
case obj.class
when Array
puts "obj is a #{obj.class}"
when String
puts "obj is a #{obj.class}"
else
puts "obj is unknown: #{obj.class}"
end
end

def if_test(obj)
print "testing via if... "
klass = obj.class
if klass == Array
puts "obj is a #{klass}"
elsif klass == String
puts "obj is a #{klass}"
else
puts "obj is unknown: #{obj.class}"
end
end

case_test(Array.new)
if_test(Array.new)
case_test(String.new)
if_test(String.new)
case_test(Hash.new)
if_test(Hash.new)
cremes$ ruby a.rb
testing via case... obj is unknown: Array
testing via if... obj is a Array
testing via case... obj is unknown: String
testing via if... obj is a String
testing via case... obj is unknown: Hash
testing via if... obj is unknown: Hash

What am I doing wrong here?
 
R

Robert Klemme

I've been using ruby for several months now so imagine my surprise when I wrote something using the 'case' construct and I discovered that I didn't understand how to use it! What's worse is that even after looking through the Pickaxe and some code "in the wild" that uses 'case' I still don't see what I'm doing wrong.

Here's the code in question:

def case_test(obj)
print "testing via case... "
case obj.class

Make that "case obj" instead.
when Array
puts "obj is a #{obj.class}"
when String
puts "obj is a #{obj.class}"
else
puts "obj is unknown: #{obj.class}"
end
end

def if_test(obj)
print "testing via if... "
klass = obj.class
if klass == Array
puts "obj is a #{klass}"
elsif klass == String
puts "obj is a #{klass}"
else
puts "obj is unknown: #{obj.class}"
end
end

case_test(Array.new)
if_test(Array.new)
case_test(String.new)
if_test(String.new)
case_test(Hash.new)
if_test(Hash.new)
cremes$ ruby a.rb
testing via case... obj is unknown: Array
testing via if... obj is a Array
testing via case... obj is unknown: String
testing via if... obj is a String
testing via case... obj is unknown: Hash
testing via if... obj is unknown: Hash

What am I doing wrong here?

robert
 
S

Stefano Crocco

Alle 18:33, luned=EC 4 dicembre 2006, (e-mail address removed) ha scritto:
def case_test(obj)
print "testing via case... "
case obj.class
when Array
puts "obj is a #{obj.class}"
when String
puts "obj is a #{obj.class}"
else
puts "obj is unknown: #{obj.class}"
end
end

Your code will call Array=3D=3D=3Dobj.class. According to ri aClass=3D=3D=
=3DanObject=20
returns true if anObject is an instance of aClass or of one of its=20
descentents (which more or less means if anObject.is_a?(aClass) returns=20
true). Because of this, all your 'when' statements will fail, because=20
obj.class is not an instance of Array or String. To do what you want, you=20
should substitute=20

case obj.class

with

case obj

Stefano
 
C

cremes.devlist

=20
Alle 18:33, luned=EC 4 dicembre 2006, (e-mail address removed) ha scritto:

Your code will call Array=3D=3D=3Dobj.class. According to ri aClass=3D=3D= =3DanObject=20
returns true if anObject is an instance of aClass or of one of its=20
descentents (which more or less means if anObject.is_a?(aClass) returns=20
true). Because of this, all your 'when' statements will fail, because=20
obj.class is not an instance of Array or String. To do what you want, you= =20
should substitute=20

case obj.class

with

case obj

Sheesh! I *knew* it was something easy. I feel like a dope...

cr
 
H

Hal Fulton

Sheesh! I *knew* it was something easy. I feel like a dope...

Don't feel like a dope. That is fairly subtle.

Consider this code:

x = foo
y = foo

case x
when y
puts "yes"
else
puts "no"
end

When would you ever expect this to print "no"?

In most cases, it will print yes. If foo is a class,
it will print no.


Hal
 

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,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top