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 A.

On May 23, 2008, at 9:40 AM, [email protected] 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. :slight_smile:

///ark

On May 23, 2008, at 12:40 PM, [email protected] wrote:

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 A.

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 B. http://agileconsultingllc.com
[email protected]

On Friday 23 May 2008, Mark W. wrote:

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

So I’m interested in the answer, too. :slight_smile:

///ark

Try Object === Foo

Stefano

On May 23, 2008, at 1:44 PM, [email protected] 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.

Thanks again,
c.

But what about X = Class? :wink:

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 B. http://agileconsultingllc.com
[email protected]

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.

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.

On May 23, 2008, at 10:55 , [email protected] 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.

On May 23, 2008, at 10:44 , [email protected] 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

[email protected] 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?

x = 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