# =\> A newb question

**URL:** <https://www.ruby-forum.com/t/a-newb-question/69269>\
**Category:** Ruby\
**Created:** [September 19, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/a-newb-question/69269 "2006-09-19T21:12:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [September 19, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/a-newb-question/69269/1 "2006-09-19T21:12:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [September 19, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/a-newb-question/69269/2 "2006-09-19T21:37:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [September 19, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/a-newb-question/69269/3 "2006-09-19T21:41:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [September 19, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/a-newb-question/69269/4 "2006-09-19T22:48:05Z")

</div>

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

John G. wrote:

> On 9/19/06, Dominic S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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. 🙂
> 
> Remember, when you’re passing a hash into a method you may often omit  
> the curlies.
> 
> —John

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [September 19, 2006, 9:45pm UTC](https://www.ruby-forum.com/t/a-newb-question/69269/5 "2006-09-19T21:45:02Z")

</div>

On 9/19/06, Dominic S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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. 🙂

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

—John

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [September 20, 2006, 1:31am UTC](https://www.ruby-forum.com/t/a-newb-question/69269/6 "2006-09-20T01:31:27Z")

</div>

> John G. wrote:
> 
> > On 9/19/06, Dominic S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [September 20, 2006, 5:19am UTC](https://www.ruby-forum.com/t/a-newb-question/69269/7 "2006-09-20T05:19:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [September 20, 2006, 5:41am UTC](https://www.ruby-forum.com/t/a-newb-question/69269/8 "2006-09-20T05:41:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [September 20, 2006, 7:05am UTC](https://www.ruby-forum.com/t/a-newb-question/69269/9 "2006-09-20T07:05:53Z")

</div>

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](http://www.rubywizards.com)
