Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Hash? But variables?

Reply
Thread Tools

Hash? But variables?

 
 
Soichi Ishida
Guest
Posts: n/a
 
      12-09-2010
Hi. I am a little confused with the use of ":" like,

hash = { :water => 'wet', :fire => 'hot' }
puts hash[:fire] # Prints: hot

in constructing the hash in this example, ":water" is the key for "wet".
I understand that. But what is the difference between "water" and
":water"?

I am guessing that ":water" works as a variable...but not quite sure.

Sorry if this is a too elementary question.

soichi

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

 
Reply With Quote
 
 
 
 
botp
Guest
Posts: n/a
 
      12-09-2010
On Thu, Dec 9, 2010 at 4:08 PM, Soichi Ishida
<(E-Mail Removed)> wrote:
> I am guessing that ":water" works as a variable...but not quite sure.


try,

:water = 1

 
Reply With Quote
 
 
 
 
Stu
Guest
Posts: n/a
 
      12-09-2010
It's actually a very good question. hashes are like arrays with non
numerical tags.

but instead of referring to the list as list[0] or list [2] you refer
to it as list[:element]

It's like having a readable array which also retains the speed.

for example of you have an array such as

a=3D[1,2,3]

you would get 2 by accessing via

a[1] (index starts at zero)

as a hash it could be written

a=3D{one: 1, two: 2, three: 3}

then to access 2 we can do

a[:two]

instead of

a[1]

btw I used the shorthand version of hash creation which drops the =3D>
and moves the colon to the other side of the key.

I hope I didn't confuse you more.

On Thu, Dec 9, 2010 at 2:08 AM, Soichi Ishida
<(E-Mail Removed)> wrote:
> Hi. =A0I am a little confused with the use of ":" like,
>
> hash =3D { :water =3D> 'wet', :fire =3D> 'hot' }
> puts hash[:fire] # Prints: =A0hot
>
> in constructing the hash in this example, ":water" is the key for "wet".
> I understand that. =A0But what is the difference between "water" and
> ":water"?
>
> I am guessing that ":water" works as a variable...but not quite sure.
>
> Sorry if this is a too elementary question.
>
> soichi
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      12-09-2010
On Thu, Dec 9, 2010 at 9:08 AM, Soichi Ishida
<(E-Mail Removed)> wrote:
> Hi. =A0I am a little confused with the use of ":" like,
>
> hash =3D { :water =3D> 'wet', :fire =3D> 'hot' }
> puts hash[:fire] # Prints: =A0hot
>
> in constructing the hash in this example, ":water" is the key for "wet".
> I understand that. =A0But what is the difference between "water" and
> ":water"?
>
> I am guessing that ":water" works as a variable...but not quite sure.


Not exactly. Here's the difference:

irb(main):001:0> :water.class
=3D> Symbol
irb(main):002:0> "water".class
=3D> String

Also:

irb(main):003:0> :water =3D=3D "water"
=3D> false
irb(main):004:0> "water" =3D=3D :water
=3D> false
irb(main):005:0> :water.eql? "water"
=3D> false

And, more importantly:

irb(main):010:0> a =3D (1..4).map { :water }
=3D> [:water, :water, :water, :water]
irb(main):011:0> a.map {|o| o.object_id}
=3D> [247864, 247864, 247864, 247864]
irb(main):012:0> a.map {|o| o.object_id}.uniq
=3D> [247864]

whereas

irb(main):013:0> a =3D (1..4).map { "water" }
=3D> ["water", "water", "water", "water"]
irb(main):014:0> a.map {|o| o.object_id}
=3D> [135636242, 135636228, 135636214, 135636200]
irb(main):015:0> a.map {|o| o.object_id}.uniq
=3D> [135636242, 135636228, 135636214, 135636200]

In other words: the sequence :water always returns the same Symbol
instance while the sequence "water" (and 'water' also) create a new
instance all the time. If you have a limited set of Hash keys the
symbol is more appropriate because it is more efficient. If the
number of keys is large and changes often better use String (e.g. when
reading them from some external source).

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Soichi Ishida
Guest
Posts: n/a
 
      12-09-2010
Thanks for all of you.

I think I am getting it, although I will need to practice to fully
understand it.

Thanks again.

soichi

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

 
Reply With Quote
 
Stu
Guest
Posts: n/a
 
      12-09-2010
I wanted to add that you should think of a hash as an accocated array.

your keys can be "strings" or :symbols

symbols are easier to read and are faster as they refer to an integer
value inside the interpreter.

hashes can hold any fundamental, complex or abstract data type and
object including other hashes.

 
Reply With Quote
 
Soichi Ishida
Guest
Posts: n/a
 
      12-09-2010
symbol and string...
This is much deeper than I thought.

Thanks everyone!

soichi

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

 
Reply With Quote
 
Su Zhang
Guest
Posts: n/a
 
      12-09-2010
Just another addition to the facts above:

Symbols are Ruby's implementation of string interning or pooling, where
all the distinct symbols, each of which being immutable, are "interned"
in a constant pool. This mechanism brings better performance (both in
time
and space) of string comparison and reduces memory usage, but also
increases the time to create strings.

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

 
Reply With Quote
 
Mike Stephens
Guest
Posts: n/a
 
      12-10-2010
One way of describing symbols is they are constants whose value is their
name.

As far as your example is concerned, the two options give you rather the
same effect. Most likely "water" is held as an object of type string
whereas :water is held as an entry in a symbol table. Quite possibly one
is more efficient that the other (either faster or taking less space)
but Ruby is not supposed to be a language that worries about such things
(it's supposed to be human oriented), and I'm not convinced you could
rely on that in all implementations.

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Want to call jscript "confirm" box from VB.NET, but NOT from a but =?Utf-8?B?SlQ=?= ASP .Net 0 03-21-2006 12:14 AM
Connect to 802.11b but not 802.11g... but used to be able to...? jbraly@gmail.com Wireless Networking 1 03-13-2006 11:05 PM
conects but wont send info but receves =?Utf-8?B?dGFuaw==?= Wireless Networking 1 01-04-2006 04:07 AM
Get DateTime.Now but exact 16 years ago..but how? ASP .Net 11 12-19-2003 10:23 AM



Advertisments