Matthew Smillie
8/9/2006 10:23:00 PM
On Aug 9, 2006, at 23:03, Joshua Muheim wrote:
> Hi all
>
> I'm terrible sorry for this newbie question, but I really can't figure
> it out. :O
>
> I want to create a method that accepts a hash of parameters:
>
> def write_email *args
> '<a href="mailto:' + args[:name] + '"></a>'
> end
>
> But when I call it...
>
> <%= write_email :name => 'info', :domain => 'atelier-schmuck.ch',
> :subject => 'www.atelier-schmuck.ch - Kontaktanfrage', :body =>
> 'Hoi du!
> :-)' %>
>
> I get this error:
>
> Symbol as array index
>
> Where's the problem? Thanks for help...
It's the splat operator in the method definition, which tells Ruby
that args is an array. You don't need it, since the hash literal in
the method call is a single hash, not a collection of separate
arguments.
def test_the_first *args
puts args.class
p args
end
test_the_first :foo => :bar, :moose => :squirrel
Array
[{:foo=>:bar, :moose=>:squirrel}]
def test_the_second args
puts args.class
p args
end
test_the_second :foo => :bar, :moose => :squirrel
Hash
{:foo=>:bar, :moose=>:squirrel}
Hope that helps.
matthew smillie.