# Hash

**URL:** <https://www.ruby-forum.com/t/hash/184896>\
**Category:** Ruby\
**Created:** [March 10, 2010, 5:34am UTC](https://www.ruby-forum.com/t/hash/184896 "2010-03-10T05:34:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhananjay1972](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e480ec/32.png) [@dhananjay1972](https://www.ruby-forum.com/u/dhananjay1972)\
**Post date:** [March 10, 2010, 5:34am UTC](https://www.ruby-forum.com/t/hash/184896/1 "2010-03-10T05:34:57Z")

</div>

Hello  
the code is like this  
hash\_fruit = {}  
hash\_fruit [‘mango’]=‘orange’  
hash\_fruit [‘banana’]=‘yellow’  
hash\_fruit [‘grapes’]=‘green’  
hash\_fruit [‘apple’] = ‘red’  
hash\_fruit .each do |key , value |  
puts key + ’ '+value  
end

and the answer is  
apple red  
banana yellow  
mango orange  
grapes green

why is it so?

---

<div class="post-metadata">

**Author:** ![dhananjay1972](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e480ec/32.png) [@dhananjay1972](https://www.ruby-forum.com/u/dhananjay1972)\
**Post date:** [March 10, 2010, 7:23am UTC](https://www.ruby-forum.com/t/hash/184896/2 "2010-03-10T07:23:35Z")

</div>

An iterator is an object that allows a programmer to traverse through  
all the elements of a collection, regardless of its specific  
implementation.

In object-oriented programming, the Iterator pattern is a design  
pattern in which iterators are used to access the elements of an  
aggregate object sequentially without exposing its underlying  
representation.

So the order in which you can access the elements in the collection is  
predefined by the underlying implementation. It is only guaranteed  
that elements can not be skipped or that a previously visited element  
can not be accessed a second time. But the order differs because of  
different implementation.

2010/3/10 Dhananjay B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![dhananjay1972](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e480ec/32.png) [@dhananjay1972](https://www.ruby-forum.com/u/dhananjay1972)\
**Post date:** [March 10, 2010, 7:52am UTC](https://www.ruby-forum.com/t/hash/184896/3 "2010-03-10T07:52:51Z")

</div>

Correct me if I’m wrong, but in 1.9 Hashes do maintain order  
information (at least they remember insertion order) but 1.8 hashes do  
not.

Aaron out.

---

<div class="post-metadata">

**Author:** ![dhananjay1972](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e480ec/32.png) [@dhananjay1972](https://www.ruby-forum.com/u/dhananjay1972)\
**Post date:** [March 10, 2010, 7:53am UTC](https://www.ruby-forum.com/t/hash/184896/4 "2010-03-10T07:53:26Z")

</div>

On Tue, Mar 9, 2010 at 10:35 PM, Dhananjay B. \<  
removed\_email\_address@domain.invalid\> wrote:

> 

Your question is ambiguous, but I’ll assume it is in regards to the  
ordering. A hash table is a data structure intended to give you fast  
(near  
constant time) access to your elements. It is based off of key/value  
pairs.  
So you put in a key/value pair, and later, you can get the value out by  
submitting the key.

It does this by deriving a number from the object’s state, to determine  
the  
(probable) location of the object in an array that it keeps internally,  
then  
looking ot see if it is there.

Because these numbers are not guaranteed to be in the same sequence you  
submitted them, the ordering within the array is not guaranteed.

So when you say hash.each, it is more interested in making sure that you  
see  
each of the elements in the hash, than it is in making sure you see them  
in  
the same order you submitted them. In this way, it is more like a set  
than  
an ordered list.

So you should think about the manner in which you are using the objects,  
and  
decide if a hash is really the data structure you are wanting. Based on  
your  
use, an array may better fit your needs.

array\_fruit = Array.new  
array\_fruit \<\< [‘mango’ ,‘orange’]  
array\_fruit \<\< [‘banana’,‘yellow’]  
array\_fruit \<\< [‘grapes’,‘green’]  
array\_fruit \<\< [‘apple’ ,‘red’]  
array\_fruit.each do |key,value|  
puts “#{key} #{value}”  
end

or

array\_fruit = [[‘mango’,‘orange’] , [‘banana’,‘yellow’] ,  
[‘grapes’,‘green’] , [‘apple’,‘red’] ]  
array\_fruit.each do |key,value|  
puts “#{key} #{value}”  
end

or

array\_fruit = [ %w(mango orange) , %w(banana yellow) , %w(grapes green)  
,  
%w(apple red) ]  
array\_fruit.each do |key,value|  
puts “#{key} #{value}”  
end

Also notice that if you switch to 1.9, then they will be ordered in the  
manner in which they were added. Hashes in Ruby 1.9 preserve insertion  
order. (

[http://img163.imageshack.us/img163/1726/picture1hrm.png](http://img163.imageshack.us/img163/1726/picture1hrm.png)
