Hi –
On Wed, 5 Mar 2008, dino d. wrote:
Thanks for the replies. How do I specify my own hasg? i.e. can a
method take two hashes, and if so, how do I specify which hash an
option goes into whne called?
ex)
def mymethod(a,b, c={}, d={})
how do i specify the call?
What you’ve got there are two required arguments and two optional
arguments. If you call that method with all four args specified, you’d
do:
mymethod(1,2, { :m => “n” }, :x => “y”)
That will give the following assignments:
a 1
b 2
c { :m => “n” }
d { :x => “y” }
Ruby has the special rule that if a hash is the last thing in the
argument list, you can leave off the curly braces. But the third
argument, in this scenario, is not the last argument, so it needs
its curly braces. Otherwise it is, as I think you’ve surmised,
impossible to tell where one hash is supposed to end and the next one
begin.
David
–
Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.rubypal.com for details. Berlin dates coming soon!