Hash? But variables?

Discussion in 'Ruby' started by Soichi Ishida, Dec 9, 2010.

  1. 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/.
    Soichi Ishida, Dec 9, 2010
    #1
    1. Advertising

  2. Soichi Ishida

    botp Guest

    On Thu, Dec 9, 2010 at 4:08 PM, Soichi Ishida
    <> wrote:
    > I am guessing that ":water" works as a variable...but not quite sure.


    try,

    :water = 1
    botp, Dec 9, 2010
    #2
    1. Advertising

  3. Soichi Ishida

    Stu Guest

    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
    <> 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/.
    >
    >
    Stu, Dec 9, 2010
    #3
  4. On Thu, Dec 9, 2010 at 9:08 AM, Soichi Ishida
    <> 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/
    Robert Klemme, Dec 9, 2010
    #4
  5. 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/.
    Soichi Ishida, Dec 9, 2010
    #5
  6. Soichi Ishida

    Stu Guest

    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.
    Stu, Dec 9, 2010
    #6
  7. Soichi Ishida, Dec 9, 2010
    #7
  8. Soichi Ishida

    Su Zhang Guest

    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/.
    Su Zhang, Dec 9, 2010
    #8
  9. 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/.
    Mike Stephens, Dec 10, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Red Orchid
    Replies:
    3
    Views:
    1,038
  2. Pieter Claassen
    Replies:
    1
    Views:
    1,106
    CBFalconer
    Aug 4, 2004
  3. Replies:
    9
    Views:
    935
  4. rp
    Replies:
    1
    Views:
    513
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    610
    David A. Black
    Jul 2, 2008
Loading...

Share This Page