I was trying to develop a definition for the === operator on a custom
class I made. It seems like that when I use case statements on an
object of the class the === operator does not get invoked. What could
I be doing wrong?
Here’s how I’m using it.
a = MyClass.new()
case a
when ‘hello’;printf "Blah…\n"
when ‘bye’ ; printf "Blah…Blah…\n"
else; printf "Blah… Blah… Blah…\n"
end
I know I have the === operator correctly defined… Because when I
explicitly use it as follows then it works:
if a === 'hello'
printf "Blah....\n"
elsif a === 'bye'
printf "Blah....Blah...\n"
else
printf "Blah... Blah... Blah...\n"
end
I was trying to develop a definition for the === operator on a custom
class I made. It seems like that when I use case statements on an
object of the class the === operator does not get invoked. What could
I be doing wrong?
Here’s how I’m using it.
a = MyClass.new()
case a
when ‘hello’;printf “Blah…\n”
when ‘bye’ ; printf “Blah…Blah…\n”
else; printf “Blah… Blah… Blah…\n”
end
I know I have the === operator correctly defined… Because when I
explicitly use it as follows then it works:
if a === 'hello'
printf "Blah....\n"
elsif a === 'bye'
printf "Blah....Blah...\n"
else
printf "Blah... Blah... Blah...\n"
end
Any clues on what I’m doing wrong?
The case operator flips the order here.
“case a; when b ;…; end” equals “if b===a then … end”
class A
def ===(o)
puts “#{self.type.to_s} === #{o.type}”
end
end
a = A.new
s = “Hello”
case s
when a #=> A === String
end
···
–
([ Kent Dahl ]/)_ ~[ http://www.stud.ntnu.no/~kentda/ ]/~
))_student/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_/ö____/ (_engineering.discipline=Computer::Technology)
It is because the order is reversed; I also got the same mistake the first
time I used Ruby. Please see Item 12 in “Things That Newcomers to Ruby
Should Know”:
I was trying to develop a definition for the === operator on a custom
class I made. It seems like that when I use case statements on an
object of the class the === operator does not get invoked. What could
I be doing wrong?
case a
when ‘hello’;printf “Blah…\n”
when ‘bye’ ; printf “Blah…Blah…\n”
else; printf “Blah… Blah… Blah…\n”
end
This is equivalent to:
if ‘hello’ === a
…
elsif ‘bye’ === a
…
else
…
end
You see? The case statement uses the argument of the when clause as the
receiver.
– Nikodemus
Hmmm… so it seems like I cannot use the case statement to test
objects of the class I created In the way I originally suggested. I
would have to put the class object in the when clause, which seems
counter-intuitive.
Wednesday, October 30, 2002, 5:37:41 PM, you wrote:
case a
when ‘hello’;printf “Blah…\n”
when ‘bye’ ; printf “Blah…Blah…\n”
else; printf “Blah… Blah… Blah…\n”
end
Hmmm… so it seems like I cannot use the case statement to test
objects of the class I created In the way I originally suggested.
you can redefine ‘===’ in String class:
class String
alias :old_eq, :===
def ===(x)
MyType === x? x.compare(self) : old_eq(x)
end
end