Trying to use define_method
inside initialize
but getting undefined_method define_method
. What am I doing wrong?
class C
Do as below :
class C
def initialize(n)
self.class.send(:define_method,n) { puts "some method #{n}" }
end
end
ob = C.new("abc")
ob.abc
# >> some method abc
Module#define_method is a private method and also a class method.Your one didn't work,as you tried to call it on the instance of C
.You have to call it on C
,using #send
in your case.