Ruby Clasess

Discussion in 'Ruby' started by Ruby Users Ruby Users, May 21, 2011.

  1. Ruby Users Ruby Users, May 21, 2011
    #1
    1. Advertising

  2. Ruby Users Ruby Users

    7stud -- Guest

    7stud --, May 21, 2011
    #2
    1. Advertising

  3. Ruby Users Ruby Users, May 22, 2011
    #3
  4. Ruby Users Ruby Users

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    class Animal

    def self.inherited(subclass)
    children << subclass
    end

    def self.children
    @children ||= []
    end

    def self.descendants
    children.map do |child|
    [child].concat child.descendants
    end.flatten
    end

    end

    Dog = Class.new Animal
    Cat = Class.new Animal
    Tabby = Class.new Cat

    RUBY_VERSION # => "1.9.2"
    Animal.children # => [Dog, Cat]
    Animal.descendants # => [Dog, Cat, Tabby]

    Cat.descendants # => [Tabby]
    Josh Cheek, May 22, 2011
    #4
  5. Ruby Users Ruby Users

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Sun, May 22, 2011 at 12:11 PM, Josh Cheek <> wrote:

    > class Animal
    >
    > def self.inherited(subclass)
    > children << subclass
    > end
    >
    > def self.children
    > @children ||= []
    > end
    >
    > def self.descendants
    > children.map do |child|
    > [child].concat child.descendants
    > end.flatten
    > end
    >
    > end
    >
    > Dog = Class.new Animal
    > Cat = Class.new Animal
    > Tabby = Class.new Cat
    >
    > RUBY_VERSION # => "1.9.2"
    > Animal.children # => [Dog, Cat]
    > Animal.descendants # => [Dog, Cat, Tabby]
    >
    > Cat.descendants # => [Tabby]
    >
    >


    Accidentally pasted the code over the entire response >.<

    I meant to say you could whip something up without too much effort (as 7stud
    said, there is no standard way).
    Josh Cheek, May 22, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    202
  2. anne001
    Replies:
    1
    Views:
    391
  3. Phrogz
    Replies:
    4
    Views:
    216
    Austin Ziegler
    Sep 6, 2006
  4. roschler
    Replies:
    0
    Views:
    174
    roschler
    Oct 16, 2006
  5. Nicholas
    Replies:
    3
    Views:
    374
    Ryan Davis
    Jan 28, 2007
Loading...

Share This Page