[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Module.===(X,X) is false

calfeld

5/23/2008 4:36:00 PM

I noticed that:

class Foo
end

Foo === Foo

returns false. In particular, I can not do:

i = Foo
case i
when Foo then ...
end

I understand why Module.=== is true when it is, but is there a good
reason for it to be false in this case? Are their any plans for this
to change?

Thanks,
Chris Alfeld

9 Answers

Mark Wilden

5/23/2008 5:08:00 PM

0

On May 23, 2008, at 9:40 AM, calfeld@mac.com wrote:

> I noticed that:
>
> class Foo
> end
>
> Foo === Foo

I was going to say that Foo is not an instance of Foo, but of Class,
and that's what === is checking. But that's wrong.

Foo === Class # => false
Foo === Object # => false

So I'm interested in the answer, too. :)

///ark

Rob Biedenharn

5/23/2008 5:15:00 PM

0


On May 23, 2008, at 12:40 PM, calfeld@mac.com wrote:

> I noticed that:
>
> class Foo
> end
>
> Foo === Foo
>
> returns false. In particular, I can not do:
>
> i = Foo
> case i
> when Foo then ...
> end
>
> I understand why Module.=== is true when it is, but is there a good
> reason for it to be false in this case? Are their any plans for this
> to change?
>
> Thanks,
> Chris Alfeld


but if you had:
i = Foo.new

Look at the docs for Class#===
------------------------------------------------------------- Module#===
mod === obj => true or false
------------------------------------------------------------------------
Case Equality---Returns true if anObject is an instance of mod or
one of mod's descendents. Of limited use for modules, but can be
used in case statements to classify objects by class.

Does that help you?

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



Stefano Crocco

5/23/2008 5:25:00 PM

0

On Friday 23 May 2008, Mark Wilden wrote:
> On May 23, 2008, at 9:40 AM, calfeld@mac.com wrote:
> > I noticed that:
> >
> > class Foo
> > end
> >
> > Foo === Foo
>
> I was going to say that Foo is not an instance of Foo, but of Class,
> and that's what === is checking. But that's wrong.
>
> Foo === Class # => false
> Foo === Object # => false
>
> So I'm interested in the answer, too. :)
>
> ///ark

Try Object === Foo

Stefano


calfeld

5/23/2008 5:40:00 PM

0

Thanks for all the feedback.

I'm aware of the usual use of Module.===, namely to check if an object
is an instance of a class or descendent of that class.

My issue is not with when Module.=== is true, my issue is with a
specific example of when it is false. Namely, that, for a class (not
an instance) X, the following is true:

(! (X === X)) && (X == X)

This surprises me, and denies me the ability to compare a class (an
instance of Class) to its possible values (instances of Class) in a
case statement.

Thanks again,
c.

Rob Biedenharn

5/23/2008 5:52:00 PM

0

On May 23, 2008, at 1:44 PM, calfeld@mac.com wrote:

> Thanks for all the feedback.
>
> I'm aware of the usual use of Module.===, namely to check if an object
> is an instance of a class or descendent of that class.
>
> My issue is not with when Module.=== is true, my issue is with a
> specific example of when it is false. Namely, that, for a class (not
> an instance) X, the following is true:
>
> (! (X === X)) && (X == X)
>
> This surprises me, and denies me the ability to compare a class (an
> instance of Class) to its possible values (instances of Class) in a
> case statement.
>
> Thanks again,
> c.


But what about X = Class? ;-)

You can always use 'case' like an 'if'

Foo = Class.new
[Foo, Foo.new].each_with_index do |i,position|
print "#{position}: "
case
when Foo == i
puts 'i is the class Foo'
when Foo === i
puts 'i is an instance of Foo'
end
end

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com

calfeld

5/23/2008 5:53:00 PM

0

Let me further clarify. I understand how this is false under the
current definition. For a class Foo, Foo is not an instance of Foo or
of a descendent of Foo.

What I propose/question is extending the definition to include the
case that Foo == Foo. Is there a good reason not to include this case
as true? Are there any plans to have this be true in future versions?

c.

Ryan Davis

5/23/2008 6:02:00 PM

0


On May 23, 2008, at 10:44 , calfeld@mac.com wrote:

> This surprises me, and denies me the ability to compare a class (an
> instance of Class) to its possible values (instances of Class) in a
> case statement.

Yup. That's just the way that Module#=== works.

% irb
>> class X; end
=> nil
>> X === X
=> false
>> Object === X
=> true
>> Module === X
=> true
>> Class === X
=> true
>> case X
>> when Class then puts :class
>> when Module then puts :module
>> end
class
=> nil

you could do:

case X.name
when "X" then
# ...
end

or, safer:

case obj
when Module then
case obj.name
when "X" then
# ...
end
# ...
end

Ryan Davis

5/23/2008 7:52:00 PM

0


On May 23, 2008, at 10:55 , calfeld@mac.com wrote:

> What I propose/question is extending the definition to include the
> case that Foo == Foo. Is there a good reason not to include this case
> as true? Are there any plans to have this be true in future versions?

No plans that I know of, but you're free to propose it on ruby-core@.
It'll go farther if you're able to offer a patch, but that isn't
strictly necessary.


Sebastian Hungerecker

5/24/2008 11:29:00 AM

0

calfeld@mac.com wrote:
> What I propose/question is extending the definition to include the
> case that Foo =3D=3D Foo. =A0Is there a good reason not to include this c=
ase
> as true?

x =3D String
case x
when String
puts "x is the string #{x}"
when Class
puts "x is the class #{x}"
end

This prints "x is the class String". With your change it would print
"x is the string String".

HTH,
Sebastian
=2D-=20
Jabber: sepp2k@jabber.org
ICQ: 205544826