Adding instance methods to class at runtime

Discussion in 'Ruby' started by Jon A. Lambert, Aug 29, 2005.

  1. I want to add instance methods to a class at runtine.

    $ cat testcmds.rb
    module Bar
    def hello
    puts "hello called"
    end
    end

    class Foo
    end

    Foo.include(Bar)

    Foo.new.hello

    $ ruby testcmds.rb
    testcmds.rb:10: private method `include' called for Foo:Class
    (NoMethodError)

    How can I do this?
     
    Jon A. Lambert, Aug 29, 2005
    #1
    1. Advertising

  2. Hi --

    On Tue, 30 Aug 2005, Jon A. Lambert wrote:

    > I want to add instance methods to a class at runtine.


    There's no other time to do it :)

    > $ cat testcmds.rb
    > module Bar
    > def hello
    > puts "hello called"
    > end
    > end
    >
    > class Foo
    > end
    >
    > Foo.include(Bar)


    You can reopen the class and do the include:

    class Foo
    include Bar
    end

    or you can use send:

    Foo.send("include", Bar)


    David

    --
    David A. Black
     
    David A. Black, Aug 29, 2005
    #2
    1. Advertising

  3. Jon A. Lambert

    Kirk Haines Guest

    On Monday 29 August 2005 2:52 pm, Jon A. Lambert wrote:
    > I want to add instance methods to a class at runtine.
    >
    > $ cat testcmds.rb
    > module Bar
    > def hello
    > puts "hello called"
    > end
    > end
    >
    > class Foo
    > end
    >
    > Foo.include(Bar)
    >
    > Foo.new.hello
    >
    > $ ruby testcmds.rb
    > testcmds.rb:10: private method `include' called for Foo:Class
    > (NoMethodError)
    >
    > How can I do this?


    irb(main):013:0* Foo.send:)include, Bar)
    => Foo
    irb(main):014:0> Foo.new.hello
    hello called


    Kirk Haines
     
    Kirk Haines, Aug 29, 2005
    #3
  4. David A. Black wrote:
    > or you can use send:
    >
    > Foo.send("include", Bar)
    >


    Excellent Thank you.
    I think my problem was scope then.
    Here's what I be doing and it now works like a charm.

    $ cat cmd1/cmd_bar.rb
    module Command
    def cmd_bar
    puts "cmd_bar one called"
    end
    end

    $ cat cmd2/cmd_bar.rb
    module Command
    def cmd_bar
    puts "cmd_bar two called"
    end
    end

    $ cat testcmds.rb

    class Foo
    end

    def load1
    cmds = Dir.glob("cmd1/cmd_*.rb")
    cmds.each {|c| load(c)}
    Foo.send:)include,Command)
    end

    def load2
    cmds = Dir.glob("cmd2/cmd_*.rb")
    cmds.each {|c| load(c)}
    Foo.send:)include,Command)
    end

    f = Foo.new

    load1
    f.cmd_bar

    load2
    f.cmd_bar

    $ ruby testcmds.rb
    cmd_bar one called
    cmd_bar two called

    --
    J. Lambert
     
    Jon A. Lambert, Aug 29, 2005
    #4
    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. John M. Gabriele
    Replies:
    18
    Views:
    1,158
    Steven Bethard
    Feb 18, 2005
  2. arose
    Replies:
    2
    Views:
    143
    arose
    Jun 9, 2006
  3. Vincent Fourmond

    Modules, instance methods and class methods

    Vincent Fourmond, Sep 26, 2006, in forum: Ruby
    Replies:
    5
    Views:
    155
    Trans
    Sep 27, 2006
  4. Raj Singh
    Replies:
    2
    Views:
    200
    Rick DeNatale
    May 29, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    324
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page