Using define_method to create a method that takes arguments

How does one use define_method to create a method that can take
arguments?

Is that possible?

Wes

Wes G. wrote:

How does one use define_method to create a method that can take
arguments?

Is that possible?

Wes

Use a block that has arguments:

irb(main):003:0> class << self; define_method :foo do |x, *y| p x, y;
end; end
=> #Proc:[email protected]:3(irb)
irb(main):004:0> foo 1,2,3
1
[2, 3]
=> nil

Wes G. wrote:

How does one use define_method to create a method that can take
arguments?

Is that possible?

#!/usr/bin/ruby -w

class A
define_method(:trythis) { |a,b,c| puts a,b,c }
end

a = A.new

a.trythis(“one”,“two”,“three”)
one
two
three

a.trythis()
in `trythis’: wrong number of arguments (0 for 3) (ArgumentError)