4. ruby hashes

Posted by 竹林品雨 on June 22, 2014

###4. Ruby Hashes

####4.1 Hash 简介

#####初始化一个empty Hash

    menu = {}

#####初始化一个Hash with value

每个没有赋值的key有一个默认值

 m = Hash.new("wq")
 p m                    #{}
 p m["p"]               #"wq"
menu = {
    "mian" => "12",
    "mo" => "3",
    "ji" => "5"
}

#####Hash赋值

menu = Hash[:m,"x","b","w","mr",true]
p menu          #{:m=>"x", "b"=>"w", "mr"=>true}
    
def artax
  a = [:punch, 0]
  b = [:kick, 72]
  c = [:stops_bullets_with_hands, false]
  key_value_pairs = [a, b, c] #[[:punch, 0], [:kick, 72], [:stops_bullets_with_hands, false]]
  Hash[key_value_pairs]
end
p artax #{:punch=>0, :kick=>72, :stops_bullets_with_hands=>false}

m = {}
m["q"] = "mlx"
puts m          #{"q"=>"mlx"}
m[:p] = "brj"
puts m          #{"q"=>"mlx", :p=>"brj"}    
puts m["q"]     # "mlx"
puts m[:p]      # "brj"
m["q"] = "mb"
puts m["q"]     # "mb"

注意 注意 注意

会语法错误报错

m = {"k": "s"} #会语法错误报错

正确写法

m = {"k"=>"s"} #正确  {"k"=>"x"}
m = {k: "s"}  #正确(冒号后必须有空格) {:k=>"s"}

####4.2 Hash 迭代

key,value 输出
menu = {
    "mian" => "12",
    "mo" => "3",
    "ji" => "5"
}
menu.each do |k, v|
    puts "#{k},#{v}"
end

menu.each do |k, v|
    menu[k] = "¥"+v
end

p menu  #{"mian"=>"¥12", "mo"=>"¥3", "ji"=>"¥5"}
获取hash所有的key
menu.keys #["mian", "mo", "ji"]
获取hash所有的value
menu.values #["12", "3", "5"]