Ruby 1.9.1 和1.8.6 Hash描述的差异

在哈希表的描述上ruby 1.9.1,有很很大的改变,下面是一些值得关注的部分:

RUBY_VERSION => 1.8.6

>> {'name', "Akhil"}  
=> {"name"=>"Akhil"}  
>> {'name', "Akhil"}
=> {"name"=>"Akhil"}


RUBY_VERSION => 1.9.1

>> {'name', "Akhil"}  
=> syntax error, unexpected ',', expecting tASSOC  
  
>> {name: "Akhil"}  
=> {:name=>"Akhil"}  
>> {'name', "Akhil"}
=> syntax error, unexpected ',', expecting tASSOC

>> {name: "Akhil"}
=> {:name=>"Akhil"}

新的Hash加强了对序列处理

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'}
=> {: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_VERSION => 1.9.1


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


有点像hash.inspect,更好的to_s方法

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}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.to_s
=> "b2c3d4a1"


RUBY_VERSION => 1.9.1

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


hash.each 和 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}  
>> 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}  
>> 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}


hash.select现在会返回hash而不是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}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.select{|k,v| k == :c }
=> [[:c, 3]]


RUBY_VERSION => 1.9.1


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

你可能感兴趣的:(C++,c,C#,Ruby)