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 :slight_smile: )

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.

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 :slight_smile: )

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’.

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” ??

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

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 :slight_smile: )

http://codepad.org/foNb9Pd3

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 :slight_smile: )

http://codepad.org/foNb9Pd3

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 :slight_smile: )

http://codepad.org/foNb9Pd3

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
neat about it.

-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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs