How to invoke === using custom classes and case statements

Hi all,

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?

Thanks.

case a
when 'hello';printf "Blah....\n"
when 'bye' ; printf "Blah....Blah...\n"
else; printf "Blah... Blah... Blah...\n"
end

With this, this is String#=== which is called i.e. ruby call

   "hello".===(a)
   "bye".===(a)

and not MyClass#===

Guy Decoux

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

···

On Mon, 28 Oct 2002, Khurram wrote:

case a
when ‘hello’;printf “Blah…\n”
when ‘bye’ ; printf “Blah…Blah…\n”
else; printf “Blah… Blah… Blah…\n”
end

Khurram wrote:

Hi all,

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)

Hi,

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”:

http://www.glue.umd.edu/~billtj/ruby.html#case

Regards,

Bill

···

Khurram khabibiuf@hotmail.com wrote:

Hi all,

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?

Nikodemus Siivola tsiivola@cc.hut.fi wrote in message news:Pine.GSO.4.44.0210281622230.11279-100000@kekkonen.cs.hut.fi

···

On Mon, 28 Oct 2002, Khurram wrote:

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.

Is there any other way I could do this?

Hello Khurram,

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

···


Best regards,
Bulat mailto:bulatz@integ.ru