[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Find the fully qualified name of a class from a string

Brian Candler

3/31/2007 8:49:00 AM

On Sat, Mar 31, 2007 at 05:54:42AM +0900, Nasir Khan wrote:
> Say you have a class definition in a string -
>
> str = <<EOF
> module A
> class B
> def b
> puts "hello"
> end
> end
> end
> EOF
>
> What is the easiest way to find out the fully qualified name of the class
> there, namely "A::B"

Interesting. I thought that in general you couldn't:

m = Module.new
Foo = m
Bar = m
module m
# what's the *name* of the module we are in now??
end

But actually Ruby rejects that, so you must say 'module Foo' or 'module Bar'

1 Answer

Sean O'Halpin

3/31/2007 3:36:00 PM

0

On 3/31/07, Brian Candler <B.Candler@pobox.com> wrote:
> On Sat, Mar 31, 2007 at 05:54:42AM +0900, Nasir Khan wrote:
> > Say you have a class definition in a string -
> >
> > str = <<EOF
> > module A
> > class B
> > def b
> > puts "hello"
> > end
> > end
> > end
> > EOF
> >
> > What is the easiest way to find out the fully qualified name of the class
> > there, namely "A::B"
>
> Interesting. I thought that in general you couldn't:
>
> m = Module.new
> Foo = m
> Bar = m
> module m
> # what's the *name* of the module we are in now??
> end
>
> But actually Ruby rejects that, so you must say 'module Foo' or 'module Bar'
>
I think this illustrates your point:

m = Module.new do
attr_accessor :name
end

B = Class.new do
include m
end
p B.ancestors
# [B, #<Module:0xb7c3bc2c>, Object, Kernel]

Regards,

Sean