# Hash Reverse?

**URL:** <https://www.ruby-forum.com/t/hash-reverse/177899>\
**Category:** Ruby\
**Created:** [October 26, 2009, 2:00pm UTC](https://www.ruby-forum.com/t/hash-reverse/177899 "2009-10-26T14:00:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [October 26, 2009, 2:00pm UTC](https://www.ruby-forum.com/t/hash-reverse/177899/1 "2009-10-26T14:00:11Z")

</div>

I am preparing for my orals exam in Ruby (my last chance… , If I fail  
the test, no more possible to study Computer Science related 🙂 )

[http://codepad.org/foNb9Pd3](http://codepad.org/foNb9Pd3)

class Hash  
def hash\_revert  
hash\_new = Hash.new  
self.each {|key,value|  
if not hash\_new.has\_key?(key) then hash\_new[value] = key end  
}  
return hash\_new  
end  
end

h = {2=\>“a”, 1=\> “b”, 3 =\>“a”, 4=\> “a”, 5 =\> “b”, 6=\>“c”}  
p h.hash\_revert #–\> {“a”=\>4, “b”=\>1, “c”=\>6}

or Should I get this ??

# =\> {“a”=\>2, “b”=\>1, “c”=\>6}

Because HASH is working like “Set” ??

many thanks in advance,  
salai.

---

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [October 26, 2009, 3:09pm UTC](https://www.ruby-forum.com/t/hash-reverse/177899/2 "2009-10-26T15:09:07Z")

</div>

Salai Koko wrote:

> I am preparing for my orals exam in Ruby (my last chance.. , If I fail  
> the test, no more possible to study Computer Science related 🙂 )
> 
> [http://codepad.org/foNb9Pd3](http://codepad.org/foNb9Pd3)
> 
> class Hash  
> def hash\_revert  
> hash\_new = Hash.new  
> self.each {|key,value|  
> if not hash\_new.has\_key?(key) then hash\_new[value] = key end  
> }  
> return hash\_new  
> end  
> end
> 
> h = {2=\>“a”, 1=\> “b”, 3 =\>“a”, 4=\> “a”, 5 =\> “b”, 6=\>“c”}  
> p h.hash\_revert #–\> {“a”=\>4, “b”=\>1, “c”=\>6}
> 
> or Should I get this ??
> 
> # =\> {“a”=\>2, “b”=\>1, “c”=\>6}
> 
> Because HASH is working like “Set” ??
> 
> many thanks in advance,  
> salai.

In Ruby 1.9, hashes keep the order of input. Before Ruby 1.9, the order  
is “random” - so it’s not guaranteed which will be the first key that  
you will see with ‘each’.

---

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [October 26, 2009, 3:16pm UTC](https://www.ruby-forum.com/t/hash-reverse/177899/3 "2009-10-26T15:16:18Z")

</div>

2009/10/26 Aldric G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > ```
> > if not hash_new.has_key?(key) then hash_new[value] = key end
> > 
> > ```
> > 
> > Because HASH is working like “Set” ??
> > 
> > many thanks in advance,  
> > salai.
> 
> In Ruby 1.9, hashes keep the order of input. Before Ruby 1.9, the order  
> is “random” - so it’s not guaranteed which will be the first key that  
> you will see with ‘each’.

As far as I can see salai does not want to reverse the order of  
elements but rather the key value relationship.

Salai, it really depends on the requirements. Here’s another valid  
variant:

class Hash  
def hash\_revert  
r = Hash.new {|h,k| h[k] = }  
each {|k,v| r[v] \<\< k}  
r  
end  
end

irb(main):008:0\> {2=\>“a”, 1=\> “b”, 3 =\>“a”, 4=\> “a”, 5 =\> “b”,  
6=\>“c”}.hash\_revert  
=\> {“a”=\>[2, 3, 4], “b”=\>[1, 5], “c”=\>[6]}

[http://codepad.org/XNvwvOxi](http://codepad.org/XNvwvOxi)

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [October 26, 2009, 6:53pm UTC](https://www.ruby-forum.com/t/hash-reverse/177899/4 "2009-10-26T18:53:30Z")

</div>

On Mon, Oct 26, 2009 at 8:59 AM, salai [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am preparing for my orals exam in Ruby (my last chance.. , If I fail  
> the test, no more possible to study Computer Science related 🙂 )
> 
> [http://codepad.org/foNb9Pd3](http://codepad.org/foNb9Pd3)

[codepad.org](http://codepad.org) idea is wicked, new bookmark for me =)

–  
Kind Regards,  
Rajinder Y.

[http://DevMentor.org](http://DevMentor.org)

Do Good! - Share Freely, Enrich and Empower people to Transform their  
lives.

---

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [October 26, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/hash-reverse/177899/5 "2009-10-26T17:16:55Z")

</div>

On Mon, Oct 26, 2009 at 8:59 AM, salai [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> h = {2=\>“a”, 1=\> “b”, 3 =\>“a”, 4=\> “a”, 5 =\> “b”, 6=\>“c”}  
> p h.hash\_revert #–\> {“a”=\>4, “b”=\>1, “c”=\>6}
> 
> or Should I get this ??
> 
> # =\> {“a”=\>2, “b”=\>1, “c”=\>6}
> 
> Because HASH is working like “Set” ??

See Hash#invert and perhaps its implementation.

-greg

---

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [October 26, 2009, 7:34pm UTC](https://www.ruby-forum.com/t/hash-reverse/177899/6 "2009-10-26T19:34:41Z")

</div>

Rajinder Y. wrote:

> On Mon, Oct 26, 2009 at 8:59 AM, salai [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I am preparing for my orals exam in Ruby (my last chance.. , If I fail  
> > the test, no more possible to study Computer Science related 🙂 )
> > 
> > [http://codepad.org/foNb9Pd3](http://codepad.org/foNb9Pd3)
> 
> [codepad.org](http://codepad.org) idea is wicked, new bookmark for me =)

I recommend [pastie.org](http://pastie.org) , actually.

---

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [October 26, 2009, 8:57pm UTC](https://www.ruby-forum.com/t/hash-reverse/177899/7 "2009-10-26T20:57:38Z")

</div>

On Mon, Oct 26, 2009 at 2:34 PM, Aldric G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Rajinder Y. wrote:
> 
> > On Mon, Oct 26, 2009 at 8:59 AM, salai [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > I am preparing for my orals exam in Ruby (my last chance.. , If I fail  
> > > the test, no more possible to study Computer Science related 🙂 )
> > > 
> > > [http://codepad.org/foNb9Pd3](http://codepad.org/foNb9Pd3)
> > 
> > [codepad.org](http://codepad.org) idea is wicked, new bookmark for me =)
> 
> I recommend [pastie.org](http://pastie.org) , actually.

But the two are not the same thing. Pastie is a nice pastebin  
service, for sure (I typically use it myself when I don’t need the  
features gist has to offer)  
But [codepad.org](http://codepad.org) actually executes the code pasted in. That is what is  
neat about it.

-greg

---

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [October 26, 2009, 8:16pm UTC](https://www.ruby-forum.com/t/hash-reverse/177899/8 "2009-10-26T20:16:39Z")

</div>

Good Morning,

On Mon, Oct 26, 2009 at 5:59 AM, salai [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> key end

The answer can be different (and most likely will be) under 1.8 and 1.9

- in  
1.8 order is not guaranteed - but in 1.9 it is (order of insertion into  
the  
hash) and therefore under 1.9 you will get the second option and  
apparantly  
under 1.8 you get your first.

John

---

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [October 27, 2009, 6:24pm UTC](https://www.ruby-forum.com/t/hash-reverse/177899/9 "2009-10-27T18:24:26Z")

</div>

Dear All,

Thank you very much to you all, I passed the exam.

regards,  
Salai

On Mon, Oct 26, 2009 at 8:57 PM, Gregory B.
