My friend and I were writing some code for an event. We were programming in ruby and (un)fortunately we had different versions of ruby. I had Ruby version 1.9.1 and his version was 1.8.6. We wrote a few lines, which used hash. When I tried it on my machine, it worked perfectly fine but on my friend’s machine, it was giving absurd behavior.

We actually were inserting some values in hash in an order and wanted to retrieve it in same order. This is ok in ruby 1.9.1. But if one do the same in ruby 1.8.6, ruby doesn't preserve the order in hash. On debugging and googling, we got that no ordering was the root cause for our code not working in ruby 1.8.6.

Here are some more differences in ruby 1.8.6 and 1.9.1 regarding hash.

**1. Now Hash preserves order:**

**RUBY_VERSION = > 1.8.6**

> > hash = {:a= > 'A', :b= >'B', :c= >'C', :d= >'D'}

= > {:b= >"B", :c= >"C", :d= >"D", :a= >"A"}

> > hash.to_a

= > [[:b, "B"], [:c, "C"], [:d, "D"], [:a, "A"]]

> > hash.keys

= > [:b, :c, :d, :a]

> > hash.values

= > ["B", "C", "D", "A"]

> > hash = {:a= > 'A', :b= >'B', :c= >'C', :d= >'D'}

= > {:a= >"A", :b= >"B", :c= >"C", :d= >"D"}

> > hash.to_a

= > [[:a, "A"], [:b, "B"], [:c, "C"], [:d, "D"]]

> > hash.keys

= > [:a, :b, :c, :d]

> > hash.values

= > ["A", "B", "C", "D"]

**2. Better to_s method(similar to hash.inspect).**

RUBY_VERSION = > 1.8.6

> > hash = {:a= > 1, :b= >2, :c= >3, :d= >4}

= > {:b= >2, :c= >3, :d= >4, :a= >1}

> > hash.to_s

= > "b2c3d4a1"

> > hash = {:a= > 1, :b= >2, :c= >3, :d= >4}

= > {:a= >1, :b= >2, :c= >3, :d= >4}

> > hash.to_s

= > "{:a= >1, :b= >2, :c= >3, :d= >4}"

**3. hash.each and hash.each_pair**

RUBY_VERSION = > 1.8.6

> > hash = {:a= > 1, :b= >2, :c= >3, :d= >4}

= > {:b= >2, :c= >3, :d= >4, :a= >1}

> > hash.each{|x| p x}

[:b, 2]

[:c, 3]

[:d, 4]

[:a, 1]

= > {:b= >2, :c= >3, :d= >4, :a= >1}

> > hash.each_pair{|x| p x}

(irb):48: warning: multiple values for a block parameter (2 for 1)

from (irb):48

[:b, 2]

(irb):48: warning: multiple values for a block parameter (2 for 1)

from (irb):48

[:c, 3]

(irb):48: warning: multiple values for a block parameter (2 for 1)

from (irb):48

[:d, 4]

(irb):48: warning: multiple values for a block parameter (2 for 1)

from (irb):48

[:a, 1]

= > {:b= >2, :c= >3, :d= >4, :a= >1}

RUBY_VERSION = > 1.9.1

> > hash = {:a= > 1, :b= >2, :c= >3, :d= >4}

= > {:a= >1, :b= >2, :c= >3, :d= >4}

> > hash.each{|x| p x}

[:a, 1]

[:b, 2]

[:c, 3]

[:d, 4]

= > {:a= >1, :b= >2, :c= >3, :d= >4}

> > hash.each_pair{|x| p x}

[:a, 1]

[:b, 2]

[:c, 3]

[:d, 4]

= > {:a= >1, :b= >2, :c= >3, :d= >4}

**4. hash.select now returns hash instead of array**

RUBY_VERSION = > 1.8.6

> > hash = {:a= > 1, :b= >2, :c= >3, :d= >4}

= > {:b= >2, :c= >3, :d= >4, :a= >1}

> > hash.select{|k,v| k == :c }

= > [[:c, 3]]

> > hash = {:a= > 1, :b= >2, :c= >3, :d= >4}

= > {:a= >1, :b= >2, :c= >3, :d= >4}

> > hash.select{|k,v| k == :c }

= > {:c= >3}

**Subscribe**- To get an automatic feed of all future posts subscribe here, or to receive them via email go here and enter your email address in the box. You can also like us on facebook and follow me on Twitter @akashag1001.
## 0 Comments

Post a Comment