# Iterating through a hash

**URL:** <https://www.ruby-forum.com/t/iterating-through-a-hash/146412>\
**Category:** Ruby\
**Created:** [August 20, 2008, 7:38pm UTC](https://www.ruby-forum.com/t/iterating-through-a-hash/146412 "2008-08-20T19:38:45Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 20, 2008, 7:38pm UTC](https://www.ruby-forum.com/t/iterating-through-a-hash/146412/1 "2008-08-20T19:38:45Z")

</div>

How can I iterate through a hash so that each key is modified and saved  
into  
a new hash?

# Beginning of code

hash = {“name”=\>“greg”, “job”=\>“boring”, “hair”=\>“plenty”}  
p hash

map = hash.each\_key do |key|  
key.upcase  
end

p map

# End of code

I’d imagine that this would return map as a new hash with the keys  
modified.  
Is there anything like collect! for hashes?

Brian

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 20, 2008, 7:53pm UTC](https://www.ruby-forum.com/t/iterating-through-a-hash/146412/2 "2008-08-20T19:53:22Z")

</div>

How about something like:

map = {}  
hash.each\_pair do |k,v|  
map[k.upcase] = v  
end

–wpd

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 20, 2008, 7:56pm UTC](https://www.ruby-forum.com/t/iterating-through-a-hash/146412/3 "2008-08-20T19:56:54Z")

</div>

2008/8/20 Brian R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> end
> 
> p map
> 
> # End of code

Hash[\*hash.map{|key,value| [key.upcase, value]}.flatten]

Farrel

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 20, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/iterating-through-a-hash/146412/4 "2008-08-20T20:17:25Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

#upcase doesn’t modify the receiver.

#upcase! doesn’t work because you can’t modify String keys, as they  
are frozen.

So, you have to rebuild the hash, the others pointed out some  
solutions for this.

Regards,  
Florian G.

On Aug 20, 2008, at 7:34 PM, Brian R. wrote:

> key.upcase  
> Brian  
> -----BEGIN PGP SIGNATURE-----  
> Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkisX3AACgkQJA/zY0IIRZYC0ACfVfK1H80uupfXn/g0jhp69uzB  
O4YAoMIao4OCp1WfSUkpTWKW3LQA3tKP  
=WHKL  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 20, 2008, 8:58pm UTC](https://www.ruby-forum.com/t/iterating-through-a-hash/146412/5 "2008-08-20T20:58:28Z")

</div>

On 20.08.2008 19:34, Brian R. wrote:

> end
> 
> p map
> 
> # End of code
> 
> I’d imagine that this would return map as a new hash with the keys modified.  
> Is there anything like collect! for hashes?

Folks, thanks for leaving the #inject solution to me. 🙂

map = hash.inject({}) do |h,(k,v)|  
h[k.upcase] = v  
h  
end

Cheers

robert

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 20, 2008, 9:06pm UTC](https://www.ruby-forum.com/t/iterating-through-a-hash/146412/6 "2008-08-20T21:06:06Z")

</div>

On Wed, Aug 20, 2008 at 11:26 AM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Folks, thanks for leaving the #inject solution to me. 🙂
> 
> map = hash.inject({}) do |h,(k,v)|  
> h[k.upcase] = v  
> h  
> end

without the ugly “h” at the end 🙂 :

map = hash.inject({}) {|h, (k,v)| h.update({k.upcase =\> v})}

martin

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 20, 2008, 9:52pm UTC](https://www.ruby-forum.com/t/iterating-through-a-hash/146412/7 "2008-08-20T21:52:59Z")

</div>

On Wed, Aug 20, 2008 at 2:32 PM, Martin DeMello  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> without the ugly “h” at the end 🙂 :
> 
> map = hash.inject({}) {|h, (k,v)| h.update({k.upcase =\> v})}

Thanks everyone! Using Martin’s example, how would I then use upcase for  
an  
array of hashes?

I’d imagine it would be

this\_array.collect do |hash|  
@map = {}  
hash.each\_pair do |k,v|  
@map[k.upcase] = v  
end  
end

but while the @map gives me the scope to see it outside of the block, it  
looks like only the first hash within the array is passed through.

Brian

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 20, 2008, 10:52pm UTC](https://www.ruby-forum.com/t/iterating-through-a-hash/146412/8 "2008-08-20T22:52:49Z")

</div>

On Wed, Aug 20, 2008 at 12:48 PM, Brian R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> this\_array.collect do |hash|  
> @map = {}  
> hash.each\_pair do |k,v|  
> @map[k.upcase] = v  
> end  
> end
> 
> but while the @map gives me the scope to see it outside of the block, it  
> looks like only the first hash within the array is passed through.

new\_array = this\_array.collect do |hash|  
map = {}  
hash.each\_pair do |k,v|  
map[k.upcase] = v  
end  
map  
end

collect collects the return value of the block

martin

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 21, 2008, 10:44am UTC](https://www.ruby-forum.com/t/iterating-through-a-hash/146412/9 "2008-08-21T10:44:21Z")

</div>

From: Brian R. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# I’d imagine that this would return map as a new hash with the

# keys modified. Is there anything like collect! for hashes?

the required pairings for hashes makes it fragile to implement a  
map/collect like feature similar to plain arrays.

you can create one if you like. just be careful.

eg, here is my simple-minded implementation,

irb(main):084:0\> class Hash  
irb(main):085:1\> def map2  
irb(main):086:2\> h={}  
irb(main):087:2\> self.each do |k,v|  
irb(main):088:3\* kk,vv=yield(k,v)  
irb(main):089:3\> key = kk || k  
irb(main):090:3\> val = vv || v  
irb(main):091:3\> h[key] = val  
irb(main):092:3\> end  
irb(main):093:2\> h  
irb(main):094:2\> end  
irb(main):095:1\> end  
=\> nil  
irb(main):096:0\> hash.map2{|k,\_| k.upcase}  
=\> {“NAME”=\>“greg”, “JOB”=\>“boring”, “HAIR”=\>“plenty”}  
irb(main):097:0\> hash.map2{|k| k.upcase}  
=\> {“NAME”=\>“greg”, “JOB”=\>“boring”, “HAIR”=\>“plenty”}  
irb(main):098:0\> hash.map2{|k,v| k.upcase}  
=\> {“NAME”=\>“greg”, “JOB”=\>“boring”, “HAIR”=\>“plenty”}  
irb(main):099:0\> hash.map2{|k,v| [k.upcase,v.capitalize]}  
=\> {“NAME”=\>“Greg”, “JOB”=\>“Boring”, “HAIR”=\>“Plenty”}

kind regards -botp

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 21, 2008, 9:46am UTC](https://www.ruby-forum.com/t/iterating-through-a-hash/146412/10 "2008-08-21T09:46:20Z")

</div>

2008/8/20 Martin DeMello [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> without the ugly “h” at the end 🙂 :
> 
> map = hash.inject({}) {|h, (k,v)| h.update({k.upcase =\> v})}

But with ugly and unnecessary brackets. This works as well

map = hash.inject({}) {|h, (k,v)| h.update(k.upcase =\> v)}

Note though that this unfortunately creates a lot of temporary Hashes  
that are thrown away immediately. I personally find that more ugly  
than the h at the end. You can reformat to make it less stand out as  
in

map = hash.inject {} do |h,(k,v)|  
h[k.upcase] = v; h  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 21, 2008, 1:29pm UTC](https://www.ruby-forum.com/t/iterating-through-a-hash/146412/11 "2008-08-21T13:29:20Z")

</div>

2008/8/21 Peña, Botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> irb(main):084:0\> class Hash  
> irb(main):095:1\> end  
> =\> nil

I’d like to use that bit for a small discussion if you allow. I  
believe the implementation would be better off by not messing with  
return values from the block because the changes lead to more complex  
code and results may be surprising (i.e. if you relied on returning  
nil or false and expected that to show up in the new Hash). By having  
the method do less things it becomes more flexible to use.

Another interesting question is which is the proper method for  
creating the new Hash? I can think at least of these as reasonable  
solutions

h = {}  
h = Hash.new(default)  
h = Hash.new(&default\_proc)  
h = default\_proc ? Hash.new(&default\_proc) : Hash.new(default)  
h = self.class.new  
h = self.class.new(default)  
h = self.class.new(&default\_proc)  
h = dup.clear

The solution might be to do

def map2(h = {})  
each do |k,v|  
k, v = yield k, v  
h[k] = v  
end  
h  
end

i.e. allow the caller to decide where he wants the mapped data to go  
while defaulting to the most straightforward case, a new Hash. This is  
similar to what Enumerable#map does, i.e. it creates a new Array  
regardless of the type at hand.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 30, 2008, 2:23am UTC](https://www.ruby-forum.com/t/iterating-through-a-hash/146412/12 "2008-08-30T02:23:44Z")

</div>

For fun, a solution with zip:

class Hash  
def map\_keys &block  
Hash[\* keys.map(&block).zip(values)]  
end  
end

Clay

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 22, 2008, 10:02pm UTC](https://www.ruby-forum.com/t/iterating-through-a-hash/146412/13 "2008-08-22T22:02:24Z")

</div>

Farrel L. wrote:

> 2008/8/20 Brian R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > end
> > 
> > p map
> > 
> > # End of code
> 
> Hash[\*hash.map{|key,value| [key.upcase, value]}.flatten]
> 
> Farrel

\*hash.map{|key,value| [key.upcase, value]}.flatten

what \* do to the array pls? 🙂
