Very Basic Question

G

Gilman Gunn

Hi all,

I have a very basic question... what does "=>" mean? Two books I have
looked at started using it without explaining and it is filtered in most
searches.

My guess is that it has something to do with assignment but am not sure.

If anyone could shed some light on this it would be much appreciated.

Thanks.
 
F

Farrel Lifson

2008/12/7 Gilman Gunn said:
I have a very basic question... what does "=>" mean?

It's how you assign values to keys in a hash.

letter_values = { 'a' => 1, 'b' => 2, 'c' => 3,..., z => '26' }
letter_values['b'] # 2
letter_values['j'] # 10

Farrel
 
Y

Yaser Sulaiman

[Note: parts of this message were removed to make it a legal post.]

Using => in method calls used to puzzle me. I encountered this for the first
time in FXRuby, and then in Ruby on Rails.

It turned out that Ruby allows you to give a hash when invoking a method,
where arguments to the method get rolled up into one hash variable. Consider
this example (stolen from the Ruby Programming Wikibook[0]):

def accept_hash(var)
print "got: ", var.inspect
end
accept_hash :arg1 => 'giving arg1', :argN => 'giving argN'

Hope you find this helpful.

Regards,
Yaser Sulaiman

[0] http://en.wikibooks.org/wiki/Ruby_Programming

P.S. In the 1st edition of Programming Ruby, it is mentioned that keyword
arguments "are scheduled to be implemented in Ruby 1.8". Is this it (i.e.
the keyword arguments feature), or is it something else?
 
P

Phlip

Yaser said:
def accept_hash(var)
print "got: ", var.inspect
end
accept_hash :arg1 => 'giving arg1', :argN => 'giving argN'

Better, Ruby 1.9 will let us fold the symbol and => notation:

arg1: 'giving arg1', argN: 'giving argN'

This means that Ruby has invented the "named argument" system, but it did it the
right way; by building the feature out of low-level syntax elements that we can
reuse for other situations. Ruby did not do what some languages do - invent
named arguments using their own magic system that only works in method argument
lists.
 
G

Gilman Gunn

Thx so much guys... I finally have my app up and running. No doubt more
questions will follow!
 

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,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top