Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [RCR] Module#nesting (NOT the class method - instance method)

Reply
Thread Tools

[RCR] Module#nesting (NOT the class method - instance method)

 
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      02-05-2007


harp:~ > cat a.rb
class Module
def nesting
parent, child = nil
name.split('::').inject(Object){|parent, child| parent.const_get child}
Object == parent ? nil : parent
end
end

class A
class B
class C
class D
end
end
end
end

p A.nesting
p A::B.nesting
p A::B::C.nesting
p A::B::C:.nesting


harp:~ > ruby a.rb
nil
A
A::B
A::B::C

-a
--
we can deny everything, except that we have the possibility of being better.
simply reflect on that.
- the dalai lama

 
Reply With Quote
 
 
 
 
Trans
Guest
Posts: n/a
 
      02-05-2007


On Feb 5, 3:23 pm, (E-Mail Removed) wrote:
> harp:~ > cat a.rb
> class Module
> def nesting
> parent, child = nil
> name.split('::').inject(Object){|parent, child| parent.const_get child}
> Object == parent ? nil : parent
> end
> end
>
> class A
> class B
> class C
> class D
> end
> end
> end
> end
>
> p A.nesting
> p A::B.nesting
> p A::B::C.nesting
> p A::B::C:.nesting
>
> harp:~ > ruby a.rb
> nil
> A
> A::B
> A::B::C


irb(main):001:0> require 'facets/core/module/nesting'
=> true
irb(main):002:0> class A
irb(main):003:1> class B
irb(main):004:2> class C
irb(main):005:3> class D
irb(main):006:4> end
irb(main):007:3> end
irb(main):008:2> end
irb(main):009:1> end
=> nil
irb(main):010:0> A.nesting
=> [A]
irb(main):011:0> B.nesting
NameError: uninitialized constant B
from (irb):11
from :0
irb(main):012:0> A::B.nesting
=> [A, A::B]
irb(main):013:0> A::B::C.nesting
=> [A, A::B, A::B::C]
irb(main):014:0> A::B::C:.nesting
=> [A, A::B, A::B::C, A::B::C:]

class Module
def nesting
n = []
name.split(/::/).inject(self){ |mod, name| c =
mod.const_get(name) ; n << c ; c }
return n
end
end

T.


 
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
injecting an instance method into another class' class method Chuck Remes Ruby 3 08-12-2010 07:15 PM
why can't an instance instantiated within a class method access aprotected instance method? Greg Hauptmann Ruby 9 06-16-2008 10:16 AM
instance method adding another instance method to the class Raj Singh Ruby 2 05-29-2008 10:09 PM
accessing class instance variable from instance method David Garamond Ruby 5 06-08-2004 02:26 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments