How to dynamically create instance methods at runtime?

前端 未结 3 1678
旧时难觅i
旧时难觅i 2021-01-17 18:08

[ruby 1.8]

Assume I have:

dummy \"string\" do
    puts \"thing\" 
end

Now, this is a call to a method which has as

3条回答
  •  误落风尘
    2021-01-17 18:28

    use define_method:

    class Bar 
    end
    
    bar_obj = Bar.new
    
    class << bar_obj
     define_method :new_dynamic_method do
      puts "content goes here"
     end
    end
    
    bar_obj.new_dynamic_method
    

    Output:

    content goes here
    

提交回复
热议问题