=> A newb question

D

Dominic Son

why did matz choose to use '=>' to declare things instead of just making
them all '=' ?

he cut our time marginally by letting us not put a ';' at the end of a
line, but now we have to put a '>' with '='...

(i'm gonna guess a = operator is for assigning a value, whereas a => is
for assigning symbols? )
 
P

Phlip

Dominic said:
why did matz choose to use '=>' to declare things instead of just making
them all '=' ?

= is assignment.

=> is shorthand for building a map. So :symbol => 'element' is shorthand for
{}[:symbol] = 'element', or something hard like that. So Matz again saved us
a lot of typing. (I think Perl has this feature too, but we will still
credit Matz here!)
 
J

John Gabriele

why did matz choose to use '=>' to declare things instead of just making
them all '=' ?

'=>' isn't for assignment, per se. It's the syntax you use when defining a hash:

irb(main):001:0> h = {'foo' => 3, 'bar' => 1.05457e-34}
=> {"foo"=>3, "bar"=>1.05457e-34}
irb(main):002:0> h['bar']
=> 1.05457e-34
he cut our time marginally by letting us not put a ';' at the end of a
line, but now we have to put a '>' with '='...

Not sure what you're talking about there.
(i'm gonna guess a = operator is for assigning a value, whereas a => is
for assigning symbols? )

Well, I guess with "=>", you're assigning a value to a key in a hash. :)

Remember, when you're passing a hash into a method you may often omit
the curlies.

---John
 
D

Dominic Son

Thanks for the clarification. I feel like an enlightened newbie right
now.


John said:
why did matz choose to use '=>' to declare things instead of just making
them all '=' ?

'=>' isn't for assignment, per se. It's the syntax you use when defining
a hash:

irb(main):001:0> h = {'foo' => 3, 'bar' => 1.05457e-34}
=> {"foo"=>3, "bar"=>1.05457e-34}
irb(main):002:0> h['bar']
=> 1.05457e-34
he cut our time marginally by letting us not put a ';' at the end of a
line, but now we have to put a '>' with '='...

Not sure what you're talking about there.
(i'm gonna guess a = operator is for assigning a value, whereas a => is
for assigning symbols? )

Well, I guess with "=>", you're assigning a value to a key in a hash. :)

Remember, when you're passing a hash into a method you may often omit
the curlies.

---John
 
M

Max Muermann

AFAIK, one of the things slated for inclusion in Ruby 2.0 is the use
of a colon for hash associations:

{a:1, b:100, c:1024}

Cheers,
Max
 
D

Devin Mullins

Dominic said:
Thanks for the clarification. I feel like an enlightened newbie right
now.
Keep in mind that you *can* use the = sign inside a Hash literal, which
is why it's not available for use by the Hash syntax:
hash = { :key => :value, :blah => foo = 5 }
p hash, foo
Not that that's not a really screwy thing to do...

Devin
 
M

MonkeeSage

Note also that in the context of a hash literal (i.e., { ... }) or the
explicit constructor (i.e., Hash[ ... ]) you may use commas instead the
the 'fatarrow'.

h = {:a, 'hi,', :b, 'how are', :c, 'you?'}
# or ...
# h = Hash[:a, 'hi,', :b, 'how are', :c, 'you?']
h.keys.sort.each { |k| p "#{k} => #{h[k]}" }

This can't work for implied hashes in method calls (i.e., without the
braces) because there would be no (easy, reliable) way to tell the keys
and values of the hash from normal method parameters (which are also
seperated by commas). You can make it explicit (with the { ... }) and
use the commas there too however.

Regards,
Jordan
 
M

Mike Dvorkin

It's a little known fact, but just like Perl (but unlike PHP!) you
can use commas instead of =>, for example:

$ irbSyntaxError: compile error
(irb):1: odd number list for Hash
from (irb):1=> {"on"=>"a bed", :monkeys=>"jumping", 10=>"litte"}

And since it's a hash the order is not guaranteed (again, just like
Perl, but unlike PHP).

Mike Dvorkin
http://www.rubywizards.com
 

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

Forum statistics

Threads
474,212
Messages
2,571,101
Members
47,697
Latest member
looped_monk

Latest Threads

Top