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!)
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...
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.
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...