• RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Ruby 1.9.1: Hash

In ruby 1.9.1 has many changes for Hash some useful changes are below:

RUBY_VERSION => 1.8.6

RUBY_VERSION => 1.9.1
[ruby]
>> {‘name’, "Akhil"}
=> syntax error, unexpected ‘,’, expecting tASSOC

>> {name: "Akhil"}
=> {:name=>"Akhil"}
[/ruby]

Now Hash preserves order:

RUBY_VERSION => 1.8.6
[ruby]
>> 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"]
[/ruby]
RUBY_VERSION => 1.9.1
[ruby]
>> 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"]
[/ruby]

Better to_s method(similar to hash.inspect).

RUBY_VERSION => 1.8.6
[ruby]
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.to_s
=> "b2c3d4a1"
[/ruby]
RUBY_VERSION => 1.9.1
[ruby]
>> 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}"
[/ruby]

hash.each and hash.each_pair

RUBY_VERSION => 1.8.6
[ruby]
>> 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]
RUBY_VERSION => 1.9.1
[ruby]
>> 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}
[/ruby]

hash.select now returns hash instead of array

RUBY_VERSION => 1.8.6
[ruby]
>> 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]]
[/ruby]
RUBY_VERSION => 1.9.1
[ruby]
>> 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}
[/ruby]


Asset Pipeline has been extracted a...

The asset pipeline which was introduced in Rails 3.1, has ...

ClientSideValidations: Add/Map mode...

https://twitter.com/bansalakhil/status/256699237230845952

Action and Page caching has been ex...

With the commit c82cf81f00f Action and Page caching has been extracted ...

turbo-sprockets-rails3: Speeds up ...

http://twitter.com/bansalakhil/status/253420357841723393

EdgeRails: ActiveRecord::SessionSto...

With the commit 3324e28804 ActiveRecord::SessionStore is extracted out of Rails into ...

Asset Pipeline has been extracted a...

The asset pipeline which was introduced in Rails 3.1, has ...

ClientSideValidations: Add/Map mode...

https://twitter.com/bansalakhil/status/256699237230845952

Action and Page caching has been ex...

With the commit c82cf81f00f Action and Page caching has been extracted ...

turbo-sprockets-rails3: Speeds up ...

http://twitter.com/bansalakhil/status/253420357841723393

EdgeRails: ActiveRecord::SessionSto...

With the commit 3324e28804 ActiveRecord::SessionStore is extracted out of Rails into ...