I would like to pass an argument(s) to a method being defined using define_method, how would I do that?
Ruby – How to pass arguments to define_method
metaprogrammingruby
Related Question
- Ruby – Checking if a variable is defined
- Ruby – How to pass command line arguments to a rake task
- Ruby – How to write a switch statement in Ruby
- Ruby-on-rails – Is Ruby pass by reference or by value
- Ruby – How to check if a value exists in an array in Ruby
- Ruby – When monkey patching an instance method, can you call the overridden method from the new implementation
- Ruby – define_method: How to dynamically create methods with arguments
Best Solution
The block that you pass to define_method can include some parameters. That's how your defined method accepts arguments. When you define a method you're really just nicknaming the block and keeping a reference to it in the class. The parameters come with the block. So: