From: Dhananjay Bhavsar on
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?
--
Posted via http://www.ruby-forum.com/.

From: Xiao Jia on
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 Bhavsar <dhananjaybhavsar(a)gmail.com>:
> 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?
> --
> Posted via http://www.ruby-forum.com/.
>
>

From: Aaron D. Gifford on
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.

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

On Tue, Mar 9, 2010 at 10:35 PM, Dhananjay Bhavsar <
dhananjaybhavsar(a)gmail.com> wrote:

> 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?
> --
> Posted via http://www.ruby-forum.com/.
>
>
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

From: Jesús Gabriel y Galán on
On Wed, Mar 17, 2010 at 10:22 AM, Sem Ptiri <rubyforum(a)chthonic.co.za> wrote:
> I have a hash, that stores values like this
>
> grid[[0,1] => "Some value here", [7,8] => "Some other value here, [3,4]
> => Some text here]]
>
> This gets looped through and displayed as a table.
>
> My problem is that the values are not fixed. The internal hash structure
> is variable though.
>
> I would like to the the max value for
>
> grid[[rows, column] => "value"]
>
> the rows and columns.
>
> I've looked at the base classes. Am I missing a function for doing this
> without iteration?

If I understand you correctly, you have a hash whose keys are 2
element arrays. And you want to know which is the biggest row (first
element) in all the keys and which one is the biggest column (second
element), right?

If so, you can do this (the max function iterates using Hash#each
internally, though):

irb(main):001:0> h = {[0,1] => "1 value", [7,8] => "another", [3,9] =>
"the last one"}
=> {[7, 8]=>"another", [0, 1]=>"1 value", [3, 9]=>"the last one"}
irb(main):002:0> h.max {|a,b| a[0] <=> b[0]}
=> [[7, 8], "another"]
irb(main):003:0> h.max {|a,b| a[0] <=> b[0]}[0][0]
=> 7
irb(main):004:0> h.max {|a,b| a[1] <=> b[1]}
=> [[3, 9], "the last one"]
irb(main):005:0> h.max {|a,b| a[1] <=> b[1]}[0][1]
=> 9

Hope this helps,

Jesus.

 |  Next  |  Last
Pages: 1 2
Prev: [ANN] lorax 0.1.0 Released
Next: SOCKSSocket working?