From: Robert Klemme Date: 2010-12-09T17:47:46+09:00 Subject: Re: Hash? But variables? On Thu, Dec 9, 2010 at 9:08 AM, Soichi Ishida wrote: > 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. Not exactly. Here's the difference: irb(main):001:0> :water.class => Symbol irb(main):002:0> "water".class => String Also: irb(main):003:0> :water == "water" => false irb(main):004:0> "water" == :water => false irb(main):005:0> :water.eql? "water" => false And, more importantly: irb(main):010:0> a = (1..4).map { :water } => [:water, :water, :water, :water] irb(main):011:0> a.map {|o| o.object_id} => [247864, 247864, 247864, 247864] irb(main):012:0> a.map {|o| o.object_id}.uniq => [247864] whereas irb(main):013:0> a = (1..4).map { "water" } => ["water", "water", "water", "water"] irb(main):014:0> a.map {|o| o.object_id} => [135636242, 135636228, 135636214, 135636200] irb(main):015:0> a.map {|o| o.object_id}.uniq => [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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/