James Gray
4/17/2007 6:43:00 PM
On Apr 17, 2007, at 12:59 PM, Kyle Schmitt wrote:
> Ohh. So this is technically not argument number type overloading.
> Dohh.
>
>
> ie: def foo(bar)
> def foo(bar,manchu)
> def foo(bar,manchu,biebelch)
>
> Which you can't do in straight ruby. Or can you?
Sure, multiple ways;
>> def foo(bar, manchu = nil, biebelch = nil)
>> [bar, manchu, biebelch]
>> end
=> nil
>> foo 1
=> [1, nil, nil]
>> foo 1, 2
=> [1, 2, nil]
>> foo 1, 2, 3
=> [1, 2, 3]
>> foo 1, 2, 3, 4
ArgumentError: wrong number of arguments (4 for 3)
from (irb):7:in `foo'
from (irb):7
>> def foo(*args)
>> case args.size
>> when 1 then "Called with bar: #{args.inspect}."
>> when 2 then "Called with bar and manchu: #{args.inspect}."
>> when 3 then "Called with bar, manchu and biebelch: #
{args.inspect}."
>> else raise ArgumentError, "wrong number of arguments"
>> end
>> end
=> nil
>> foo 1
=> "Called with bar: [1]."
>> foo 1, 2
=> "Called with bar and manchu: [1, 2]."
>> foo 1, 2, 3
=> "Called with bar, manchu and biebelch: [1, 2, 3]."
>> foo 1, 2, 3, 4
ArgumentError: wrong number of arguments
from (irb):25:in `foo'
from (irb):31
Hope that helps.
James Edward Gray II