# Hash Reverse?

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 )

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β ??

salai.

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 )

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β ??

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β.

2009/10/26 Aldric G. [email protected]:

``````  if not hash_new.has_key?(key) then hash_new[value] = key end
``````

Because HASH is working like βSetβ ??

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]}

Kind regards

robert

On Mon, Oct 26, 2009 at 8:59 AM, salai [email protected] 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 )

codepad.org idea is wicked, new bookmark for me =)

β
Kind Regards,
Rajinder Y.

http://DevMentor.org

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

On Mon, Oct 26, 2009 at 8:59 AM, salai [email protected] 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

Rajinder Y. wrote:

On Mon, Oct 26, 2009 at 8:59 AM, salai [email protected] 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 )

codepad.org idea is wicked, new bookmark for me =)

I recommend pastie.org , actually.

On Mon, Oct 26, 2009 at 2:34 PM, Aldric G. [email protected]
wrote:

Rajinder Y. wrote:

On Mon, Oct 26, 2009 at 8:59 AM, salai [email protected] 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 )

codepad.org idea is wicked, new bookmark for me =)

I recommend 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 actually executes the code pasted in. That is what is

-greg

Good Morning,

On Mon, Oct 26, 2009 at 5:59 AM, salai [email protected] 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

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.