Hi –
Actually this:
Class === obj.type
is always true, because it’s testing to see whether obj.type is a
Class.
Maybe you could do:
if Class === const_get(s)
obj = s.new
end
it seems to be a valid test for me?
irb(main):002:0> Class === (eval s).type
NameError: (eval):1:in irb_binding': undefined local variable or method
foobar’ for #Object:0x40190ce0 from (irb):2
from (irb):2
Well, admittedly Class === obj.type is only true if obj is a Ruby
object
In your example, the eval raises a NameError, so the
=== test is never even performed.
irb(main):003:0> s = ‘String’
“String”
irb(main):004:0> Class === (eval s).type
true
The #type method always returns a Class object, and this:
Class === SomeClass
is always true. So
Class === some_object.type
will always be true.
In the example we’re discussing, the syntax sort of chases its own
tail, in the sense that we’ve got a String “String”, eval’ing which
yields the Class object String – which, of course, is the class of
the thing we just eval’d… See if this little battery of tests
helps clarify it:
irb(main):001:0> s = ‘String’
“String”
irb(main):002:0> s.type
String
irb(main):003:0> eval(s)
String
irb(main):004:0> eval(s).type
Class
irb(main):005:0> Class === s
false
irb(main):006:0> Class === s.type
true
irb(main):007:0> Class === eval(s)
true
irb(main):008:0> Class === eval(s).type
true
irb(main):009:0> s.type == eval(s)
true
Note the one false result: s itself is a String, not a Class. But the
class of s is a Class, and the class of String (namely, Class) is also
a Class.
David
···
On Wed, 20 Nov 2002, ahoward wrote:
On Wed, 20 Nov 2002 dblack@candle.superlink.net wrote:
–
David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav