hash

D

Dhananjay Bhavsar

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?
 
X

Xiao Jia

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.
 
A

Aaron D. Gifford

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.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

On Tue, Mar 9, 2010 at 10:35 PM, Dhananjay Bhavsar <
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?
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,588
Members
45,093
Latest member
Vinaykumarnevatia00

Latest Threads

Top