How do you pass arguments to define_method?

Sixty4Bit picture Sixty4Bit · Sep 18, 2008 · Viewed 58.3k times · Source

I would like to pass an argument(s) to a method being defined using define_method, how would I do that?

Answer

Kevin Conner picture Kevin Conner · Sep 18, 2008

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:

define_method(:say_hi) { |other| puts "Hi, " + other }