标签:style blog color ar sp 文件 div on log
创建一个Hash
names = Hash.new names["chn"] = "LiuLei" names["eng"] = "Voctrals" names["jpn"] = "ryourayi"
names = {"chn" => "Liulei", "eng" => "Voctrals", "jpn" =>"ryourayi"}
#创建一个有默认值的hash names3 = Hash.new("") p names3["any_thing"] #=> "" #创建一个没有默认值的hash names4 = Hash.new p names4["any_thing"] #=> nil
p names4 = Hash.new names4.store("xi", "jinping") p names4.fetch("xi") #=>jinping p names4.fetch("liu") #=>会抛异常 p names4.fetch("liu", "voctrals") #=>voctrals,第二个参数作为默认值返回
取出Hash对象的所有的键和值
names5 = {"chn" => "Liulei", "eng" => "Voctrals", "jpn" =>"ryourayi"} p names5.keys #=>["chn", "eng", "jpn"] p names5.values #=>["Liulei", "Voctrals", "ryourayi"]
Hash转化为数组
names6 = {"chn" => "Liulei", "eng" => "Voctrals", "jpn" =>"ryourayi"} p names6.to_a #=>[["chn", "Liulei"], ["eng", "Voctrals"], ["jpn", "ryourayi"]]
each相关(不明白each_value有啥用!!!)
names6 = {"chn" => "Liulei", "eng" => "Voctrals", "jpn" =>"ryourayi"} names6.each{|key, value| print "when ", key, " values ", value, ".\n" } names6.each_key{|key| print "when ", key, " values ", names6[key], ".\n" }
提供默认值
# method one
names = Hash.new(1) names.store("a", 97) p names["a"] #=>97 p names["b"] #=>1 #method two
names2 = Hash.new{|hash, key| hash[key] = key } names2.store("a", 100) p names2["a"] #=>100 p names2["b"] #=>"b"
#method three
names3 = {}
p names3.fetch("a", "voctrals") #=>"voctrals"
是否含有某个key
names2 = Hash.new{|hash, key| hash[key] = key } names2.store("a", 100) p names2.key?("b") #=>false p names2.has_key?("b") #=>false p names2.include?("b") #=>false p names2.member?("b") #=>false
是否含有某个value
names2 = Hash.new{|hash, key| hash[key] = key } names2.store("a", 100) p names2.value?(100) #=>true p names2.has_value?(100) #=>true p names2.has_value?(200) #=>false
查询Hash的大小,是否为空
names2 = Hash.new{|hash, key| hash[key] = key } names2.store("a", 100) p names2.size #=>1,与length相同 p names2.empty? #=>false
删除一个键值对
names = {"chn" => "Liulei", "eng" => "Voctrals", "jpn" =>"ryourayi"} p names.size #=>3 p names.delete("chn") #=>"Liulei",返回值为删除掉的value p names.length #=>2
p names.delete("chn2"){|key| "NOT EXSIT #{key}"} #=>"NOT EXSIT chn2",当不存在key的时候触发后面的代码块
条件删除
names = {"chn" => "Liulei", "eng" => "Voctrals", "jpn" =>"ryourayi"} p names.delete_if{|key| key=="jpn"} #=>{"chn"=>"Liulei", "eng"=>"Voctrals"} p names #=>{"chn"=>"Liulei", "eng"=>"Voctrals"}
names = {"chn" => "Liulei", "eng" => "Voctrals", "jpn" =>"ryourayi"} p names.delete_if{|key, value| key=="jpn" || value=="Voctrals"} #=>{"chn"=>"Liulei"} p names #=>{"chn"=>"Liulei"}
names = {"chn" => "Liulei", "eng" => "Voctrals", "jpn" =>"ryourayi"} p names.reject!{|key, value| key=="jpnX" || value=="VoctralsX"} #=>nil,没有被删除时返回值为nil,而delete_if的返回值为原Hash p names #=>{"chn" => "Liulei", "eng" => "Voctrals", "jpn" =>"ryourayi"}
清空Hash,仅仅针对于调用clear的hash对象,和h = Hash.new效果是一样的
names = {"chn" => "Liulei", "eng" => "Voctrals", "jpn" =>"ryourayi"} p names.clear #=>{} p names #=>{}
取出文件中各个单词出现的次数
count = Hash.new(0) while line = gets words = line.split words.each{|word| count[word] += 1 } end count.sort{|a, b| #sort,原理是count.to_a,获得数组,所有的数组再分配给a,b #<=>当a[1]>b[1]返回正数,相等返回0,小于返回负数 a[1] <=> b[1] }.each{|key, value| print key, " appears ", value, " times.", "\n" }
标签:style blog color ar sp 文件 div on log
原文地址:http://www.cnblogs.com/voctrals/p/4048852.html