# Iterate HashWithIndifferentAccess

**URL:** <https://www.ruby-forum.com/t/iterate-hashwithindifferentaccess/195334>\
**Category:** Rails\
**Created:** [September 21, 2010, 9:23pm UTC](https://www.ruby-forum.com/t/iterate-hashwithindifferentaccess/195334 "2010-09-21T21:23:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [September 21, 2010, 9:23pm UTC](https://www.ruby-forum.com/t/iterate-hashwithindifferentaccess/195334/1 "2010-09-21T21:23:37Z")

</div>

How do I iterate a HashWithIndifferentAccess? I need to set the order  
using a sortable\_element.

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [September 21, 2010, 10:46pm UTC](https://www.ruby-forum.com/t/iterate-hashwithindifferentaccess/195334/2 "2010-09-21T22:46:24Z")

</div>

first check out the class of the object, if its a hash do

HashWithIndifferentAccess.each do |key, value|  
puts key  
puts value  
end

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [September 21, 2010, 10:49pm UTC](https://www.ruby-forum.com/t/iterate-hashwithindifferentaccess/195334/3 "2010-09-21T22:49:55Z")

</div>

On 21 September 2010 21:45, radhames brito [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> first check out the class of the object, if its a hash do
> 
> HashWithIndifferentAccess.each do |key, value|  
> puts key  
> puts value  
> end

if it’s a hash, its order is not guaranteed… next time you iterate  
it, you might find it’s in a different order

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [September 21, 2010, 11:01pm UTC](https://www.ruby-forum.com/t/iterate-hashwithindifferentaccess/195334/4 "2010-09-21T23:01:19Z")

</div>

PÃ¥l BergstrÃ¶m wrote:

> How do I iterate a HashWithIndifferentAccess? I need to set the order  
> using a sortable\_element.

As Michael said, order is not guaranteed – even if you include an  
element for that.

When I need to count on the order I use an array of pairs. I use this  
for value list data. It’s not as handy as Lasso’s array of pairs which  
are sortable and searchable, but it’s a close as we get.

list = [[“Small”, “S”], [“Medium”,“M”], [“Large”,“L”] ]

If you have to start with the Hash, and it includes a sort number of  
some type, then you can modify the conversion to specifically retrieve  
the by the sort number.

If you need help with that, give an exampe of the exact data structure  
you’d be starting with.

– gw

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [September 22, 2010, 10:43am UTC](https://www.ruby-forum.com/t/iterate-hashwithindifferentaccess/195334/5 "2010-09-22T10:43:33Z")

</div>

Greg W. wrote:

> If you need help with that, give an exampe of the exact data structure  
> you’d be starting with.

With the help of sortable\_element and :tree set as true (and moving up  
item 3 before item 1, just as a test) I get a Hash from this list:

- 
- 
- 
  - 

- 

The hash:

{“0”=\>{“id”=\>“3”}, “1”=\>{“id”=\>“1”}, “2”=\>{“0”=\>{“id”=\>“6”}, “id”=\>“5”,  
“1”=\>{“id”=\>“7”}}}

I can sort out the key and value. But with key nr 2 I have problem (id 5  
with sub list containing id 6). If I use params[:mylist].each do |k,v| I  
only get the key of 2 and value 5. How do I deal with a nested hash like  
that?

I hope I make sense.

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [September 22, 2010, 2:41pm UTC](https://www.ruby-forum.com/t/iterate-hashwithindifferentaccess/195334/6 "2010-09-22T14:41:55Z")

</div>

radhames brito wrote:

> i hash inside a hash i read it list[:outer\_value][:deeper\_value] and  
> you  
> can do
> 
> list.each do |key,value|  
> if value.class == ‘Hash’  
> puts “values for key #{ key}”  
> value.each do | key2,value2|  
> puts “key: #{key2} and value: #{value2}”  
> end  
> else  
> puts “key: #{key} and value: #{value}”  
> end  
> end

Perfect. Thanks.

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [September 22, 2010, 1:29pm UTC](https://www.ruby-forum.com/t/iterate-hashwithindifferentaccess/195334/7 "2010-09-22T13:29:14Z")

</div>

i hash inside a hash i read it list[:outer\_value][:deeper\_value] and  
you  
can do

list.each do |key,value|  
if value.class == ‘Hash’  
puts “values for key #{ key}”  
value.each do | key2,value2|  
puts “key: #{key2} and value: #{value2}”  
end  
else  
puts “key: #{key} and value: #{value}”  
end  
end

2010/9/22 PÃ¥l BergstrÃ¶m [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
