Newbie: Symbol as array index error

Hi all

I’m terrible sorry for this newbie question, but I really can’t figure
it out. :open_mouth:

I want to create a method that accepts a hash of parameters:

def write_email *args

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

On Aug 9, 2006, at 23:03, Joshua M. wrote:

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.

Thanks a lot, I got it now. :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs