weird keyword parameters in rails

R

robbie

Hi, I'm very new to Ruby, and to Ruby on Rails.
I'm halfway through the pickaxe book, past all of the stuff on syntax,
and I haven't seen anything explaining the keyword parameter syntax
used extensively in rails. The syntax I mean is:

method( :keyword => "value" )

I understand how to use this, I'm just not sure where it comes from.
Am I being docile, or is this slightly weird?
Thanks

robbie
 
R

robbie

just wanted to add, can't google it as google doesn't seem to search on
non alphanumeric characters. If anyone knows how to do that in google I
would also much appreciate it (sorry this is off topic).
:)
 
A

Austin Ziegler

Hi, I'm very new to Ruby, and to Ruby on Rails.
I'm halfway through the pickaxe book, past all of the stuff on syntax,
and I haven't seen anything explaining the keyword parameter syntax
used extensively in rails. The syntax I mean is:
=20
method( :keyword =3D> "value" )
=20
I understand how to use this, I'm just not sure where it comes from.
Am I being docile, or is this slightly weird?

It's a hash with a Symbol as a parameter.

Try it in irb.

def foo(bar)
puts bar.class
puts bar.inspect
end

foo:)keyword =3D> 'value')

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 
R

rmagick

Check out p. 361 in the Pickaxe 2nd Ed. In a method call, the initial
parameters may be followed by "a list of key => value pairs. These
pairs are collected into a single new Hash object and passed as a
single parameter." In your example, "method" would get a hash argument
with 1 key-value pair.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top