extend an object using string as module name?

Discussion in 'Ruby' started by ako..., Dec 9, 2005.

  1. ako...

    ako... Guest

    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
    ako..., Dec 9, 2005
    #1
    1. Advertising

  2. ako...

    Guest

    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
    --
    ===============================================================================
    | ara [dot] t [dot] howard [at] noaa [dot] gov
    | all happiness comes from the desire for others to be happy. all misery
    | comes from the desire for oneself to be happy.
    | -- bodhicaryavatara
    ===============================================================================
    , Dec 9, 2005
    #2
    1. Advertising

  3. ako...

    Ross Bamford Guest

    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}")

    --
    Ross Bamford -
    "\e[1;31mL"
    Ross Bamford, Dec 9, 2005
    #3
  4. On Fri, Dec 09, 2005 at 12:50:40PM +0900, wrote:
    > 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


    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...

    --
    Mauricio Fernandez
    Mauricio Fernández, Dec 9, 2005
    #4
  5. ako...

    Trans Guest

    require 'facet/kernel/constant'

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

    T.
    Trans, Dec 9, 2005
    #5
  6. ako...

    Guest

    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
    --
    ===============================================================================
    | ara [dot] t [dot] howard [at] noaa [dot] gov
    | all happiness comes from the desire for others to be happy. all misery
    | comes from the desire for oneself to be happy.
    | -- bodhicaryavatara
    ===============================================================================
    , Dec 9, 2005
    #6
    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. ding feng
    Replies:
    2
    Views:
    2,782
    ding feng
    Jun 25, 2003
  2. Replies:
    10
    Views:
    1,008
    Jochen Schulz
    Jul 9, 2006
  3. Ilariu Raducan
    Replies:
    2
    Views:
    339
    Ilariu Raducan
    Jul 14, 2004
  4. Maric Michaud
    Replies:
    0
    Views:
    7,178
    Maric Michaud
    Jun 24, 2006
  5. Andrea Reginato

    Extend an object with module stored

    Andrea Reginato, Jan 31, 2006, in forum: Ruby
    Replies:
    4
    Views:
    86
    Andrea Reginato
    Feb 1, 2006
Loading...

Share This Page