ruby hash操作

定义一个空hash

h = {}

定义一个有值的hash

h={:key1 => "value1", key2:"value2","key3" => "value3"}  #=>{:key1=>"value1", :key2=>"value2", "key3"=>"value3"}

给hash添加key-valueh["key1"] = "value"

h[:key2] ="value2"

h.store("a",1) #=>1 相当于h["a"]=1

获取值

h.fetch("a") 与h["a"]等价

删除全部的hash,两种方法

h.clear

h={}

shift方法随机删除k-v对。

h = { :a  => 1,  :b  => 2,  :c  => 3}   
h.shift             #=> [:a, 1]    
h                     #=> {:b=>2, :c=>3}    
a = h.shift       #=> [:c, 3]    
a                     #=> [:c, 3]

 

delete, delete_if, reject,reject! 方法删除指定的k-v对:
h = { :a  => 1,  :b  => 2}   
h.delete(:a )                      #=> 1    
h                                     #=> {:b=>2}    
h = {:a  => 1,  :b  => 2}       #=> {:a=>1, :b=>2}    
h.delete_if {|k,v| v != 3}   #=> {}    
h                                     #=> {}    
h = {:a  => 1,  :b  => 2}       #=> {:a=>1, :b=>2}    
h.delete_if {|k,v| v != 1}   #=> {:a=>1}    
h = {:a  => 1,  :b  => 2}       #=> {:a=>1, :b=>2}    
h.reject {|k,v| v!=2}          #=> {:b=>2}    
h                                     #=> {:a=>1, :b=>2}  
reject方法相当于dup.delete_if{}

五。 Hash的迭代:
有each, each_key, each_value ,each_pair(each方法的别名)
不举例了。


六。检测Hash中的key,value:
   检测是否有key:
     has_key?(include? 别名, 只能判断key! ) ,key?, member? 
   检测是否有value:
     has_value? value?

七。将散列转换为数组:
h = { :a  => 1,  :b  => 2}   
h.to_a         #=> [[:a, 1], [:b, 2]]   转换为一个二维数组。    
h.keys         #=> [:a, :b]    
h.values      #=> [1,2]    
下面的方法有用点:
h = { :a  => 1,  :b  =>  '2' ,  :c  => 5}   
h.values_at(:a , :b )     #=> [1, “2”]    根据指定的key返回对应values的数组    

八。根据条件选择key-value对:
h.detect {|k,v| v ==  "2"  }              #=> [:b, "2" ]   
detect和find是别名关系,是Enumerable模块里的方法,Hash类mixin这个模块,所以也可以用。select 方法,别名是find_all,可以返回多个匹配的k-v对:

h.select {|k,v| v.is_a?( Integer )}    #=> [[:a, 1], [:c, 5]] 
九。 Hash的排序:
    可以直接用sort方法,不过会返回一个二维数组。
    值得注意的是,当Hash的key是Symbol类型的时候,sort方法会出错。


十。 合并两个Hash:
   使用merge方法,( merge!和update是别名关系 (thx Beck) )  。

h1 = { :a  => 1,  :b  => 2}           #=> {:a=>1, :b=>2}    
h2 = {:b  => 3,  :d  => 3}           #=> {:d=>3, :b=>3}    
h1.merge h2                         #=> {:a=>1, :d=>3, :b=>3}    
h2.merge h1                         #=> {:a=>1, :d=>3, :b=>2}    
  注意看:b值的变化。 
 当然我们可以使用block来改变这一结局:
  h1.merge h2  do   |k,old, new  |   
      old < new  ? old :  new    
  end    
#=> {:a=>1, :d=>3, :b=>2}    
h1.merge h2 do  |k,old, new  |   
    p old   
    p new    
end    
#=>2    
#=>3  
(当然我们可以使用rails里active_support实现的revert_merge来实现上述效果)。

十一。 数组转换为Hash:
  当数组元素为偶数个数的时候:
arr = %w[a b c d]          #=> ["a" , "b" , "c" , "d" ]    
h = Hash [*arr]               #=> {"a" =>"b" , "c" =>"d" } 
此时数组必须为偶数个元素。

十二。 当Hash的key是动态变化的时候:
x = [1,2]                   #=> [1, 2]    
h = {x => 2}            #=> {[1, 2]=>2}    
h[x]                         #=> 2    
x[0] = 5                   #=> 5    
h[x]                         #=> nil    
h.rehash                 #=> {[5, 2]=>2}    
h[x]                         #=> 2 

 

参考链接:http://www.cnblogs.com/keen-allan/articles/2467671.html

posted @ 2016-01-12 22:38  caofz  阅读(1289)  评论(0)    收藏  举报