[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

=> A newb question

Dominic Son

9/19/2006 7:12:00 PM

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




--
Posted via http://www.ruby-....

9 Answers

Phlip

9/19/2006 7:35:00 PM

0

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

--
Phlip
http://www.greencheese.u... <-- NOT a blog!!!


Paul Lutus

9/19/2006 7:40:00 PM

0

Dominic Son 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.

--
Paul Lutus
http://www.ara...

jmg3000

9/19/2006 7:43:00 PM

0

On 9/19/06, Dominic Son <dominicson@gmail.com> 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

Dominic Son

9/19/2006 8:48:00 PM

0

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


John Gabriele wrote:
> On 9/19/06, Dominic Son <dominicson@gmail.com> 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


--
Posted via http://www.ruby-....

Jeff Schwab

9/19/2006 8:55:00 PM

0

Phlip wrote:
> Dominic Son 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!)

Perl's double-barrel arrow actually does more: It puts its left-hand
argument in double-quote context. If you don't want that, you use
commas instead of arrows. I've often wondered why Ruby doesn't have the
same feature.

Max Muermann

9/19/2006 11:30:00 PM

0

> John Gabriele wrote:
> > On 9/19/06, Dominic Son <dominicson@gmail.com> 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

Devin Mullins

9/20/2006 3:19:00 AM

0

Dominic Son 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

MonkeeSage

9/20/2006 3:40:00 AM

0

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

Mike Dvorkin

9/20/2006 5:05:00 AM

0

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 Dvorkin
http://www.rubyw...


On Sep 19, 2006, at 8:18 PM, Devin Mullins wrote:

> Dominic Son 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
>