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

On Wed, Apr 18, 2007 at 02:44:23AM +0900, 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)

def get_some_records(year=Time.now.year)

On 17/04/07, shawn bright [email protected] wrote:

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?

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

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

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

Shawn B. 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

On Apr 17, 2007, at 12:59 PM, Kyle S. 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 G. II

Kyle S. 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

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

well ok, thanks!

On 4/17/07, Kyle S. [email protected] 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

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, James Edward G. II [email protected] wrote:

On Apr 17, 2007, at 2:17 PM, Robert D. 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 :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 Apr 17, 2007, at 2:17 PM, Robert D. 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 G. II