define_method: How to dynamically create methods with arguments

前端 未结 3 1835
梦谈多话
梦谈多话 2020-12-30 20:05

I want to create a bunch of methods for a find_by feature. I don\'t want to write the same thing over and over again so I want to use metaprogramming.

Say I want to

3条回答
  •  一个人的身影
    2020-12-30 20:24

    It if you read the examples here http://apidock.com/ruby/Module/define_method you will find this one:

    define_method(:my_method) do |foo, bar| # or even |*args|
      # do something
    end
    

    is the same as

    def my_method(foo, bar)
       # do something
    end
    

提交回复
热议问题