[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: how to have a default argument

Kyle Schmitt

4/17/2007 6:00:00 PM

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?

--Kyle

6 Answers

James Gray

4/17/2007 6:43:00 PM

0

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

Roseanne Zhang

4/17/2007 6:53:00 PM

0

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?
>
> --Kyle

You are right!

[quote]Finally, I am not against "method overloading", but it very
easily leads to optional static typing in the language, which changes
the language
very drastically, since you need to specify "type" to overload
arguments. Without well-thought design, it can "destroy" the language
and its culture. I have not yet got that well-thought design of
method overloading. --Matz[/quote]

http://beust.com/weblog/archives/0...

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

Kyle Schmitt

4/17/2007 7:08:00 PM

0

Wow. That's quite useful. And quite the opposite of what I've read
somewhere.
My own fault for reading and not experimenting :)

--Kyle

Robert Dober

4/17/2007 7:17:00 PM

0

On 4/17/07, Kyle Schmitt <kyleaschmitt@gmail.com> wrote:
> Wow. That's quite useful. And quite the opposite of what I've read
> somewhere.
> My own fault for reading and not experimenting :)
>
> --Kyle
>
>
I am not sure I am reading you correctly as I do not see the context
in your posts, let me try to clarify with some code though, but the
bottom line is:

There is no method overloading in Ruby.

508/8 > cat over.rb && ./over.rb
#!/usr/local/bin/ruby
# vim: sts=2 sw=2 expandtab nu tw=0:

def a
puts 'a with no param'
end

a

def a b
puts "a(#{b})"
end

a 42
a
a with no param
a(42)
/over.rb:15:in `a': wrong number of arguments (0 for 1) (ArgumentError)
from ./over.rb:15


HTH
Robert

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

James Gray

4/17/2007 7:25:00 PM

0

On Apr 17, 2007, at 2:17 PM, Robert Dober wrote:

> There is no method overloading in Ruby.

But, there are the tools to build something like it, if you desire
it. See the multi gem, for one quasi-related example.

James Edward Gray II

Robert Dober

4/17/2007 7:40:00 PM

0

On 4/17/07, James Edward Gray II <james@grayproductions.net> wrote:
> On Apr 17, 2007, at 2:17 PM, Robert Dober wrote:
>
> > There is no method overloading in Ruby.
>
> But, there are the tools to build something like it, if you desire
> it. See the multi gem, for one quasi-related example.
James I felt that there was some doubt I wanted to clarify, but it was
difficult because I could not see the context of the posts.
Did I say something stupid?...
..again ;)

Ok I'll try to be clearer, an object can have only one method with the
same name, methods are stored with names and not with signature
information.
Overloading can be simulated of course, just wanted to talk about Core Ruby.

Robert.
>
> James Edward Gray II
>
>


--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw