Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Adding instance methods to class at runtime

Reply
Thread Tools

Adding instance methods to class at runtime

 
 
Jon A. Lambert
Guest
Posts: n/a
 
      08-29-2005
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?



 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      08-29-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
Kirk Haines
Guest
Posts: n/a
 
      08-29-2005
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


 
Reply With Quote
 
Jon A. Lambert
Guest
Posts: n/a
 
      08-29-2005
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



 
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
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
instance method adding another instance method to the class Raj Singh Ruby 2 05-29-2008 10:09 PM
Modules, instance methods and class methods Vincent Fourmond Ruby 5 09-27-2006 01:23 PM
Class Methods and Instance Methods arose Ruby 2 06-09-2006 07:32 PM
difference between class methods and instance methods John M. Gabriele Python 18 02-18-2005 05:17 PM



Advertisments