Connecting Tech Pros Worldwide Forums | Help | Site Map

Hash

Newbie
 
Join Date: Oct 2007
Posts: 1
#1: Oct 16 '07
Hello guys,

How does Hash works internally,
Case1:
Expand|Select|Wrap|Line Numbers
  1. # Literal Hash
  2. h0 = { ’one’ => 1, ’two’ => 2, ’three’ => 3} 
Case 2:
Expand|Select|Wrap|Line Numbers
  1. # Populating a hash
  2. h1 = Hash.new » {}
  3. h1[’gemstone’] = ’ruby’ 
  4. h1[’fruit’] = ’banana’ 
When I try to print h0 in case 1:,
it prints as below
{"three"=>3, "two"=>2, "one"=>1}

Whereas in the Case 2 h1 prints as
{"gemstone"=>"ruby", "fruit"=>"banana"}


Why is the differences???

Thanks,
Veda

Expert
 
Join Date: May 2007
Posts: 213
#2: Oct 16 '07

re: Hash


I'm not sure I understand your question, but hashes are not order specific. So, even though you define it as in case 1 (1,2,3) it may print out in a different order, as you have seen. The hash itself is unchanged, it just runs through the items in it's own order.
Reply