To dynamically define an instance method in ruby:
1 2 3 4 5 6 7 8 9 10 11
| class Human def say_hello puts %\instance says 'hello'\ end end
message = 'hello world' Human.send(:define_method, :say_hello) { puts %\instance says '#{message}'\ }
Human.new.say_hello # >> instance says 'hello world'
|
No comments:
Post a Comment