Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby Clasess

Reply
Thread Tools

Ruby Clasess

 
 
Ruby Users Ruby Users
Guest
Posts: n/a
 
      05-21-2011
How do I get all the classes that descend from a parent class ? I found
solution on
http://stackoverflow.com/questions/2...-class-in-ruby.

But I want to know, is there a standard method or not?

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      05-21-2011
No there is not.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Ruby Users Ruby Users
Guest
Posts: n/a
 
      05-22-2011
7stud -- wrote in post #1000123:
> No there is not.


THX

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Josh Cheek
Guest
Posts: n/a
 
      05-22-2011
[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]

 
Reply With Quote
 
Josh Cheek
Guest
Posts: n/a
 
      05-22-2011
[Note: parts of this message were removed to make it a legal post.]

On Sun, May 22, 2011 at 12:11 PM, Josh Cheek <(E-Mail Removed)> 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).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ruby extension (C++) on OS X [ruby 1.8.2] and Google-Sketchup [ruby 1.8.5] Nicholas Ruby 3 01-28-2007 01:48 AM
The Ruby Edge - Digg for Ruby and Ruby On Rails roschler Ruby 0 10-15-2006 11:35 PM
ruby-talk, comp.lang.ruby, ruby-talk-google Phrogz Ruby 4 09-06-2006 06:43 PM
#!/usr/bin/ruby , #!/usr/bin/ruby -w , #!/usr/bin/ruby -T?, #!/usr/bin/ruby -T1... anne001 Ruby 1 04-23-2006 03:02 PM
[ANN] ruby-freedb, ruby-serialport, ruby-mp3info moved to Rubyforge guillaume.pierronnet@ratp.fr Ruby 0 08-31-2003 11:57 PM



Advertisments