Afficher un message
Vieux 30/03/2008, 20h34   #5
David A. Black
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby 1.9 named arguments

Hi --

On Mon, 31 Mar 2008, Tim Hunter wrote:

> bparanj@gmail.com wrote:
>> When I run the following in the irb session, I am getting an error.
>> Any ideas why? TIA.
>> irb(main):036:0> { :a => 1, :b => 4 }
>> => {:a=>1, :b=>4}
>> irb(main):037:0> { :x 2, :y 6 }
>> SyntaxError: (irb):37: syntax error, unexpected tINTEGER, expecting
>> tASSOC
>> { :x 2, :y 6 }
>> ^

>
> I think you're confusing symbol hash keys with 1.9's named arguments:
>
> foo(:a => 1, :b => 2) # symbol hash keys
> foo(a:1, b:2) # 1.9 named arguments


They're still symbols in a hash, though. Just the notation is
different:

irb(main):004:0> def x(a); p a; end
=> nil
irb(main):005:0> x(x: 1, y: 2)
{:x=>1, :y=>2}


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.rubypal.com for details and updates!

  Réponse avec citation
 
Page generated in 0,04848 seconds with 9 queries