reflecting on objects without creating one

K

_Kevin

Let's say I have two constants

a = Integer
b = Numeric

Does anyone have any idea how to see if 'a' is a subclass of 'b'
WITHOUT creating an object of class 'a'?

_Kevin
 
J

Jeremy McAnally

(Class).superclass should do it.

Example:
if a.superclass == b
puts "Yes."
else
puts "No."
end

--Jeremy
 
J

James Edward Gray II

Let's say I have two constants

a = Integer
b = Numeric

Does anyone have any idea how to see if 'a' is a subclass of 'b'
WITHOUT creating an object of class 'a'?
=> true

Hope that helps.

James Edward Gray II
 
E

Ezra Zygmuntowicz

Let's say I have two constants

a = Integer
b = Numeric

Does anyone have any idea how to see if 'a' is a subclass of 'b'
WITHOUT creating an object of class 'a'?


irb(main):021:0> a = Numeric
=> Numeric
irb(main):022:0> b = Integer
=> Integer
irb(main):023:0> a.ancestors
=> [Numeric, Comparable, Object, Kernel]
irb(main):024:0> a.ancestors.include? b
=> false
irb(main):025:0> b.ancestors.include? a
=> true
irb(main):026:0>

"b.ancestors.include? a" seems to do it. Notice I reversed your
definitions
of "a" & "b" (not for any particular reason, that's just how it
worked out).

Justin

You can also use < and > to compare subclasses or superclasses

ez $ irb# => Integer
# => Numeric
# => true
# => false


Cheers-

-- Ezra Zygmuntowicz
-- Lead Rails Evangelist
-- (e-mail address removed)
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)
 
K

_Kevin

Thanks guys,

_Kevin

Ezra said:
Let's say I have two constants

a = Integer
b = Numeric

Does anyone have any idea how to see if 'a' is a subclass of 'b'
WITHOUT creating an object of class 'a'?


irb(main):021:0> a = Numeric
=> Numeric
irb(main):022:0> b = Integer
=> Integer
irb(main):023:0> a.ancestors
=> [Numeric, Comparable, Object, Kernel]
irb(main):024:0> a.ancestors.include? b
=> false
irb(main):025:0> b.ancestors.include? a
=> true
irb(main):026:0>

"b.ancestors.include? a" seems to do it. Notice I reversed your
definitions
of "a" & "b" (not for any particular reason, that's just how it
worked out).

Justin

You can also use < and > to compare subclasses or superclasses

ez $ irb# => Integer
# => Numeric
# => true
# => false


Cheers-

-- Ezra Zygmuntowicz
-- Lead Rails Evangelist
-- (e-mail address removed)
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,221
Messages
2,571,133
Members
47,747
Latest member
swapote

Latest Threads

Top