首页 > 代码库 > Ruby Hash
Ruby Hash
创建一个Hash
names = Hash.newnames["chn"] = "LiuLei"names["eng"] = "Voctrals"names["jpn"] = "ryourayi"
names = {"chn" => "Liulei", "eng" => "Voctrals", "jpn" =>"ryourayi"}
#创建一个有默认值的hashnames3 = Hash.new("")p names3["any_thing"] #=> ""#创建一个没有默认值的hashnames4 = Hash.newp names4["any_thing"] #=> nil
p names4 = Hash.newnames4.store("xi", "jinping")p names4.fetch("xi") #=>jinpingp 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"] #=>97p names["b"] #=>1#method two
names2 = Hash.new{|hash, key| hash[key] = key}names2.store("a", 100)p names2["a"] #=>100p 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") #=>falsep names2.has_key?("b") #=>falsep names2.include?("b") #=>falsep names2.member?("b") #=>false
是否含有某个value
names2 = Hash.new{|hash, key| hash[key] = key}names2.store("a", 100)p names2.value?(100) #=>truep names2.has_value?(100) #=>truep 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 #=>3p names.delete("chn") #=>"Liulei",返回值为删除掉的valuep 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=http://www.mamicode.com/="Voctrals"} #=>{"chn"=>"Liulei"}p names #=>{"chn"=>"Liulei"}
names = {"chn" => "Liulei", "eng" => "Voctrals", "jpn" =>"ryourayi"}p names.reject!{|key, value| key=="jpnX" || value=http://www.mamicode.com/="VoctralsX"} #=>nil,没有被删除时返回值为nil,而delete_if的返回值为原Hashp 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 }endcount.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"}
Ruby Hash
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。