PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Module.===(X,X) is false
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Module.===(X,X) is false

Réponse
 
LinkBack Outils de la discussion
Vieux 23/05/2008, 17h35   #1
calfeld@mac.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Module.===(X,X) is false

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

  Réponse avec citation
Vieux 23/05/2008, 18h07   #2
Mark Wilden
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Module.===(X,X) is false

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

  Réponse avec citation
Vieux 23/05/2008, 18h14   #3
Rob Biedenharn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Module.===(X,X) is false


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 you?

-Rob

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



  Réponse avec citation
Vieux 23/05/2008, 18h25   #4
Stefano Crocco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Module.===(X,X) is false

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


  Réponse avec citation
Vieux 23/05/2008, 18h40   #5
calfeld@mac.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Module.===(X,X) is false

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.

  Réponse avec citation
Vieux 23/05/2008, 18h51   #6
Rob Biedenharn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Module.===(X,X) is false

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://agileconsultingllc.com
Rob@AgileConsultingLLC.com

  Réponse avec citation
Vieux 23/05/2008, 18h52   #7
calfeld@mac.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Module.===(X,X) is false

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.

  Réponse avec citation
Vieux 23/05/2008, 19h02   #8
Ryan Davis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Module.===(X,X) is false


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

  Réponse avec citation
Vieux 23/05/2008, 20h52   #9
Ryan Davis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Module.===(X,X) is false


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.


  Réponse avec citation
Vieux 24/05/2008, 12h29   #10
Sebastian Hungerecker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Module.===(X,X) is false

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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h18.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18987 seconds with 18 queries