How does one use define_method to create a method that can take
arguments?
Is that possible?
Wes
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:0xb7ca3f20@: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)
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs