=> A newb question

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? )

Dominic S. wrote:

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

Dominic S. wrote:

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

Umm, that isn’t what ‘=>’ does.

a = b # assign b’s value to a

a => b # associate key a with value b

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 unless you know what it does.

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


#!/usr/bin/ruby -w

a = { “name” => “Elvis”, “status” => “Left the building”, “date” => 1977
}

a.keys.each do |key|
print “#{key} => #{a[key]}\n”
end


Output:

status => Left the building
name => Elvis
date => 1977

Note that the output order doesn’t reflect the input order.

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

John G. wrote:

On 9/19/06, Dominic S. [email protected] wrote:

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. :slight_smile:

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

—John

On 9/19/06, Dominic S. [email protected] wrote:

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. :slight_smile:

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

—John

John G. wrote:

On 9/19/06, Dominic S. [email protected] wrote:

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

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

Dominic S. wrote:

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

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

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

$ irb

a = { 10, ‘litte’, :monkeys, ‘jumping’, ‘on a bed’ }
SyntaxError: compile error
(irb):1: odd number list for Hash
from (irb):1

a = { 10, ‘litte’, :monkeys, ‘jumping’, ‘on’, ‘a bed’ }
=> {“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 D.
http://www.rubywizards.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs