define_method inside a module

M

MR Damien

hi,

I am trying to use some dynamic features of ruby.

Inside my module, I try to add some method dynamicaly but I get an error
when I am trying to.



module Rubyhaviour
def add(object)
name = "@" + object.class.downcase

if self.instance_variable_get(name)
self.instance_variable_get(name) << object
else
self.instance_variable_set(name, Array.new)
self.instance_variable_get(name) << object

define_method(name) do
instance_variable_get("@#{name}")
end
end
end
end
undefined method `define_method' for #<Test:0x2ce631c> (NoMethodError)
 
M

MR Damien

MR said:
hi,

I am trying to use some dynamic features of ruby.

Inside my module, I try to add some method dynamicaly but I get an error
when I am trying to.



module Rubyhaviour
def add(object)
name = "@" + object.class.downcase

if self.instance_variable_get(name)
self.instance_variable_get(name) << object
else
self.instance_variable_set(name, Array.new)
self.instance_variable_get(name) << object

define_method(name) do
instance_variable_get("@#{name}")
end
end
end
end
undefined method `define_method' for #<Test:0x2ce631c> (NoMethodError)

I pushed enter too fast, here are the missing code

class Test
include Rubyhaviour
end

test = test.add(some_object)
Then I get "undefined method `define_method' for #<Test:0x2ce631c>
(NoMethodError)"
 
J

Joel VanderWerf

MR said:
hi,

I am trying to use some dynamic features of ruby.

Inside my module, I try to add some method dynamicaly but I get an error
when I am trying to.



module Rubyhaviour
def add(object)
name = "@" + object.class.downcase

if self.instance_variable_get(name)
self.instance_variable_get(name) << object
else
self.instance_variable_set(name, Array.new)
self.instance_variable_get(name) << object

define_method(name) do
instance_variable_get("@#{name}")
end
end
end
end
undefined method `define_method' for #<Test:0x2ce631c> (NoMethodError)

Try working in the singleton class:

class C
def foo
class << self
define_method :bar do puts "BAR"; end
end
end
end

c = C.new
c.foo
c.bar
 
M

MR Damien

Joel said:
Try working in the singleton class:

class C
def foo
class << self
define_method :bar do puts "BAR"; end
end
end
end

c = C.new
c.foo
c.bar

Hi,

seems not to work neither


------------------
def add(object)
name = "@" + self.collection_name_for(object)

if self.instance_variable_get(name)
self.instance_variable_get(name) << object
else
self.instance_variable_set(name, Array.new)
self.instance_variable_get(name) << object

class << self
define_method(name) do
instance_variable_get("@#{name}")
end
end

end
end
 
D

David A. Black

Hi --

Hi,

seems not to work neither


------------------
def add(object)
name = "@" + self.collection_name_for(object)

if self.instance_variable_get(name)
self.instance_variable_get(name) << object
else
self.instance_variable_set(name, Array.new)
self.instance_variable_get(name) << object

class << self
define_method(name) do
instance_variable_get("@#{name}")
end
end

end
end

That's because name is not in scope inside the class definition body
(class << self). In order to keep name in scope, you need to use
class_eval on the singleton class. That way, you're operating inside a
code block, which does share the local variables.

(class << self; self; end).class_eval do
define_method(name) do

etc.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
R

Robert Klemme

2008/10/20 David A. Black said:
Hi --



That's because name is not in scope inside the class definition body
(class << self). In order to keep name in scope, you need to use
class_eval on the singleton class. That way, you're operating inside a
code block, which does share the local variables.

(class << self; self; end).class_eval do
define_method(name) do

Also, watch closely the contents of "name" and how you (OP) use it.

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top