Forum: Ruby extend an object using string as module name?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A70b7da5a3a712e800100e61ef8d8917?d=identicon&s=25 akonsu (Guest)
on 2005-12-09 03:44
(Received via mailing list)
hello,

is this a proper way to do it:

name = 'MyModule'
o = Object.new
o.extend(Module.const_get(name))


are there better ways?

thanks
konstantin
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 ara.t.howard (Guest)
on 2005-12-09 04:54
(Received via mailing list)
On Fri, 9 Dec 2005, ako... wrote:

> hello,
>
> is this a proper way to do it:
>
> name = 'MyModule'
> o = Object.new
> o.extend(Module.const_get(name))
>
>
> are there better ways?

this will fail for "ModuleA::ModuleB".  for that you need something
like:

     #
     # creates a class by class name
     #
       def klass_stamp(hierachy, *a, &b)
#--{{{
         ancestors = hierachy.split(%r/::/)
         parent = Object
         while((child = ancestors.shift))
           klass = parent.const_get child
           parent = klass
         end
         klass::new(*a, &b)
#--}}}
       end

this will work for

   m = klass_stamp "A::B::C::Module"
   o = Object::new
   o.extend m

it's in alib.rb btw.

hth.

-a
Ca93f661f56856c8b49c3751bcb8cf15?d=identicon&s=25 ross (Guest)
on 2005-12-09 13:49
(Received via mailing list)
On Thu, 08 Dec 2005 18:37:43 -0800, ako... wrote:

> hello,
>
> is this a proper way to do it:
>
> name = 'MyModule'
> o = Object.new
> o.extend(Module.const_get(name))
>
>

I don't know if this is the proper way, but you could just eval the
name:

name = 'MyModule'
o = Object.new
o.extend( eval name )

Or alternatively:

eval("o.extend #{name}")
Dd76a12d66f843de5c5f8782668e7127?d=identicon&s=25 mfp (Guest)
on 2005-12-09 15:06
(Received via mailing list)
On Fri, Dec 09, 2005 at 12:50:40PM +0900, ara.t.howard@noaa.gov wrote:
>           klass = parent.const_get child
>           parent = klass
>         end
>         klass::new(*a, &b)
> #--}}}
>       end

def klass_stamp(name, *a, &b)
  name.split(/::/).inject(Object){|s,x| s.const_get(x)}.new(*a,&b)
end

class A; class B; def foo; "A::B#foo" end end end
klass_stamp("A::B").foo                            # => "A::B#foo"

> this will work for
>
>   m = klass_stamp "A::B::C::Module"
#           ^
# that would try to instantiate the module
    m = "A::B::C::Module".split(/::/).inject(Object){|s,x|
s.const_get(x)}
>   o = Object::new
>   o.extend m

PS: have you seen
http://eigenclass.org/hiki.rb?Usable+Ruby+folding+for+Vim ?
It might be of interest to you if you often use manual markers for
methods...
45196398e9685000d195ec626d477f0e?d=identicon&s=25 transfire (Guest)
on 2005-12-09 15:43
(Received via mailing list)
require 'facet/kernel/constant'

  name = 'MyModule'
  o = Object.new
  o.extend(constant(name))

T.
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 ara.t.howard (Guest)
on 2005-12-09 16:16
(Received via mailing list)
On Fri, 9 Dec 2005, Trans wrote:

>  require 'facet/kernel/constant'
>
>  name = 'MyModule'
>  o = Object.new
>  o.extend(constant(name))
>
> T.

we have dueling libraries tom!  ;-)

guess the fact that we are both  writing these function over and over
means
they are useful.

cheers.

-a
This topic is locked and can not be replied to.