N
Nilesh Trivedi
[Note: parts of this message were removed to make it a legal post.]
How do I define a singleton method dynamically?
I have code similar to this:
def foo(obj,label)
def obj.name; label; end #we want to define a singleton method called
"name" on obj which returns the value in label
puts obj.name #this is an example. Actually I have some code here which
expects this singleton method in obj
end
When I try to run this as foo(Object.new,"xyz"), it throws a "NameError:
undefined local variable or method `label' for #<Object:0xb7b02018>" whereas
I want it to print "xyz" to screen.
Of course, line 2 in method foo is not correct. But what is the correct way?
How do I define a singleton method dynamically?
I have code similar to this:
def foo(obj,label)
def obj.name; label; end #we want to define a singleton method called
"name" on obj which returns the value in label
puts obj.name #this is an example. Actually I have some code here which
expects this singleton method in obj
end
When I try to run this as foo(Object.new,"xyz"), it throws a "NameError:
undefined local variable or method `label' for #<Object:0xb7b02018>" whereas
I want it to print "xyz" to screen.
Of course, line 2 in method foo is not correct. But what is the correct way?