How to have a default argument

hello all,

i have a function that sometimes needs to be passed a variable, but
sometimes not.

def get_some_records(year)
    get records for year
    process them
    return result
end

the thing is, i already have it written, but i would like to have a default
like if get_some_records() is called, year would be assumed to be 2007

is there a way i can do this without having to change everywhere it is
called in my code?

thanks
sk

hello all,

i have a function that sometimes needs to be passed a variable, but
sometimes not.

def get_some_records(year)

def get_some_records(year=Time.now.year)

···

On Wed, Apr 18, 2007 at 02:44:23AM +0900, shawn bright wrote:

    get records for year
    process them
    return result
end

the thing is, i already have it written, but i would like to have a default
like if get_some_records() is called, year would be assumed to be 2007

is there a way i can do this without having to change everywhere it is
called in my code?

thanks
sk

--
Jos Backus
jos at catnook.com

Hi

If I understand you correctly, you could do this:

def get_some_records(year='2007')
    get records for year
    process
   return
end

year will be set to 2007 if no parameter is given.

Kjersti

···

On 17/04/07, shawn bright <nephish@gmail.com> wrote:

hello all,

i have a function that sometimes needs to be passed a variable, but
sometimes not.

def get_some_records(year)
    get records for year
    process them
    return result
end

the thing is, i already have it written, but i would like to have a
default
like if get_some_records() is called, year would be assumed to be 2007

is there a way i can do this without having to change everywhere it is
called in my code?

I don't think you can do that in straight ruby. No overloading of
that sort in ruby.

Err, but I think that if it's written as a c-extension to ruby it might work.

I could be completely wrong on both counts though.

--Kyle

Shawn Bright wrote:

hello all,

i have a function that sometimes needs to be passed a variable, but
sometimes not.

def get_some_records(year)
    get records for year
    process them
    return result
end

the thing is, i already have it written, but i would like to have a
default
like if get_some_records() is called, year would be assumed to be 2007

is there a way i can do this without having to change everywhere it is
called in my code?

thanks
sk

Assign the default value to your argument:

def get_some_records(year=2007)
   ...
end

Then, if no argument is provided when calling your method, it will
default to 2007.

David

···

--
Posted via http://www.ruby-forum.com/\.

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

well ok, thanks!

···

On 4/17/07, David Mullet <david.mullet@gmail.com> wrote:

Shawn Bright wrote:
> hello all,
>
> i have a function that sometimes needs to be passed a variable, but
> sometimes not.
>
> def get_some_records(year)
> get records for year
> process them
> return result
> end
>
> the thing is, i already have it written, but i would like to have a
> default
> like if get_some_records() is called, year would be assumed to be 2007
>
> is there a way i can do this without having to change everywhere it is
> called in my code?
>
> thanks
> sk

Assign the default value to your argument:

def get_some_records(year=2007)
   ...
end

Then, if no argument is provided when calling your method, it will
default to 2007.

David
http://rubyonwindows.blogspot.com

--
Posted via http://www.ruby-forum.com/\.

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

···

On Apr 17, 2007, at 12:59 PM, Kyle Schmitt wrote:

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/000042.html

···

--
Posted via http://www.ruby-forum.com/\.

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

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

···

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 :slight_smile:

--Kyle

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

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

···

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

There is no method overloading in Ruby.

> 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 :wink:

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.

···

On 4/17/07, James Edward Gray II <james@grayproductions.net> wrote:

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

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