case when and class

U

Une Bévue

I've found in case / when if the case is about a class :

case my_var.class
when String then puts my_var
when Array then my_var.each {|v| puts v}
end

doesn't work i do have to use :

case my_var.class.to_s
when 'String' then puts my_var
when 'Array' then my_var.each {|v| puts v}
end

why ?

(with ruby 1.9.x)
 
R

Rimantas Liubertas

I've found in case / when if the case is about a class :
case my_var.class
when String then puts my_var
when Array =C2=A0then my_var.each {|v| puts v}
end

doesn't work i do have to use :

case my_var.class.to_s
when 'String' then puts my_var
when 'Array' =C2=A0then my_var.each {|v| puts v}
end

why ?

"when" condition in case statement uses =3D=3D=3D to compare objects. For
strings a =3D b is true if a and b have the same characters, however
String =3D=3D=3D String is false:

=3D> false

BTW "=3D=3D=3D" is a method:
=3D> true

so you can define your own rules when objects should be considered equal:

class Marble
attr_accessor :size, :color

def initialize(size, color)
self.size =3D size
self.color =3D color
end

def =3D=3D=3D(other)
self.color =3D=3D other.color
end

end

m1 =3D Marble.new(0.5, "red")
m2 =3D Marble.new(0.5, "green")
m3 =3D Marble.new(0.7, "red")

case m1
when m2
puts "Matching marble is m2"
when m3
puts "Matching marble is m3"
else
puts "No match"
end

The result is "Matching marble is m3"

Regards,
Rimantas
 
J

Jesús Gabriel y Galán

2010/6/11 Une B=E9vue said:
I've found in case / when if the case is about a class :

case my_var.class
when String then puts my_var
when Array =A0then my_var.each {|v| puts v}
end

doesn't work

case calls the method =3D=3D=3D on the when object passing the case object
as an argument. So the above is calling:

String.=3D=3D=3D (my_var.class)

If you check this:

http://ruby-doc.org/core/classes/Module.html#M001666

it says it returns true if the object is *an instance* of this class.
So you don't have to pass the class, but the object itself:

case my_var
when String then puts my_var
when Array then my_var.each {|v| puts v}
end

irb(main):003:0> def test var
irb(main):004:1> case var
irb(main):005:2> when String then puts var
irb(main):006:2> when Array then var.each {|v| puts v}
irb(main):007:2> end
irb(main):008:1> end
=3D> nil
irb(main):009:0> test "hello"
hello
=3D> nil
irb(main):010:0> test [1,2,3]
1
2
3
=3D> [1, 2, 3]


i do have to use :

case my_var.class.to_s
when 'String' then puts my_var
when 'Array' =A0then my_var.each {|v| puts v}
end

You can also do this, but the above is cleaner.

Jesus.
 
R

Rimantas Liubertas

For strings a = b is true if a and b have the same characters

err, I ment a === b . For strings a === b is the same as a == b.
That's mostly true for other objects too (if they don't override ===
),
but not always.


Regards,
Rimantas
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top