[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Newbie: Symbol as array index error

Joshua Muheim

8/9/2006 10:04:00 PM

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

--
Posted via http://www.ruby-....

2 Answers

Matthew Smillie

8/9/2006 10:23:00 PM

0

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.

Joshua Muheim

8/10/2006 1:54:00 PM

0

Thanks a lot, I got it now. :-)

--
Posted via http://www.ruby-....