[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

reflecting on objects without creating one

Kevin

12/20/2006 9:35:00 PM

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

4 Answers

Jeremy McAnally

12/20/2006 9:50:00 PM

0

(Class).superclass should do it.

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

--Jeremy

On 12/20/06, _Kevin <kevin.olbrich@gmail.com> wrote:
> 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
>
>
>

James Gray

12/20/2006 9:52:00 PM

0

On Dec 20, 2006, at 3:40 PM, _Kevin wrote:

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

>> A = Integer
=> Integer
>> B = Numeric
=> Numeric
>> A.ancestors.include? B
=> true

Hope that helps.

James Edward Gray II

Ezra Zygmuntowicz

12/20/2006 10:06:00 PM

0


On Dec 20, 2006, at 1:53 PM, Justin Bailey wrote:

> On 12/20/06, _Kevin <kevin.olbrich@gmail.com> wrote:
>>
>> 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
>> a = Integer
# => Integer

>> b = Numeric
# => Numeric

>> a < b
# => true

>> a > b
# => false


Cheers-

-- Ezra Zygmuntowicz
-- Lead Rails Evangelist
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)



Kevin

12/21/2006 12:23:00 AM

0

Thanks guys,

_Kevin

Ezra Zygmuntowicz wrote:
> On Dec 20, 2006, at 1:53 PM, Justin Bailey wrote:
>
> > On 12/20/06, _Kevin <kevin.olbrich@gmail.com> wrote:
> >>
> >> 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
> >> a = Integer
> # => Integer
>
> >> b = Numeric
> # => Numeric
>
> >> a < b
> # => true
>
> >> a > b
> # => false
>
>
> Cheers-
>
> -- Ezra Zygmuntowicz
> -- Lead Rails Evangelist
> -- ez@engineyard.com
> -- Engine Yard, Serious Rails Hosting
> -- (866) 518-YARD (9273)