Is there a way to write a function with named parameters?
Something like:
Header(‘title’ => “Somr Title”)
I gess one option is to use a hash and make the function call:
Header { ‘title’ => “Somr Title”}
Is there any other way?
How about default values for parameters? How can I do those?
Thanks a lot,
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
Is there a way to write a function with named parameters?
Something like:
Header { ‘title’ => “Somr Title”}
Is there any other way?
Thi sis the way given in the Pickaxe.
http://ruby-central.com/book/tut_methods.html
How about default values for parameters? How can I do those?
Shown in the same page I gave above.
def foo(bar,baz=“Baz”)
puts “Foo, #{bar}, #{baz]”
end
foo(“hello”)
···
On Wed, 18 Dec 2002, Daniel Carrera wrote:
Foo, hello, Baz
As for combining the two - having default values for hash arguments
passed, that’s quite a bit tricker. only way I can think of is
def foo(params)
params[“baz”] = “Baz” unless params[“baz”]
params[“bar”] = “beer” unless params[“bar”]
…
end
The problem with this method is, if a programmer wants to pass ‘nil’ or
‘false’ or any non-true value as an argument, the default values would
overwrite that.
Hope that helps,
Greg
Thanks a lot,
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
–
Greg Millam
walker at deafcode.com
Is there a way to write a function with named parameters?
Check out
http://www.rubygarden.org/ruby?KeywordArguments
It covers default and mandatory arguments as well.
Cheers,
Gavin
···
From: “Daniel Carrera” dcarrera@math.umd.edu
Gavin Sinclair wrote:
From: “Daniel Carrera” dcarrera@math.umd.edu
Is there a way to write a function with named parameters?
Check out
http://www.rubygarden.org/ruby?KeywordArguments
It covers default and mandatory arguments as well.
Cheers,
Gavin
Dan Berger also did a really good presentation on this topic at the Ruby
Conference. Hopefully those presentations will be available soon at the
RubyConf web site (or wherever).
Greg Millam wrote:
As for combining the two - having default values for hash arguments
passed, that’s quite a bit tricker. only way I can think of is
def foo(params)
params[“baz”] = “Baz” unless params[“baz”]
params[“bar”] = “beer” unless params[“bar”]
…
end
The problem with this method is, if a programmer wants to pass ‘nil’ or
‘false’ or any non-true value as an argument, the default values would
overwrite that.
def foo(params)
{:argA => “default A”, :argB => “default B”}.update params
end
p foo(:argA=>nil) # ==> {:argA=>nil, :argB=>“default B”}
Lyle Johnson wrote:
Gavin Sinclair wrote:
From: “Daniel Carrera” dcarrera@math.umd.edu
Is there a way to write a function with named parameters?
Check out
http://www.rubygarden.org/ruby?KeywordArguments
It covers default and mandatory arguments as well.
Cheers,
Gavin
Dan Berger also did a really good presentation on this topic at the Ruby
Conference. Hopefully those presentations will be available soon at the
RubyConf web site (or wherever).
Thanks Lyle. 
It was my impression Ryan Davis was going to put them up somewhere at some
point, but I haven’t seen them posted.
Ryan? Any word?
Regards,
Dan