[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Using define_method to create a method that takes arguments

Wes Gamble

8/30/2006 5:04:00 AM

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

Is that possible?

Wes

--
Posted via http://www.ruby-....

2 Answers

Joel VanderWerf

8/30/2006 5:08:00 AM

0

Wes Gamble 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@(irb):3>
irb(main):004:0> foo 1,2,3
1
[2, 3]
=> nil

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Paul Lutus

8/30/2006 5:37:00 AM

0

Wes Gamble 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)

--
Paul Lutus
http://www.ara...