使用长度为4的键查找键值对

时间:2016-01-18 19:40:01

标签: ruby hash

Ruby文件:

student_ages = {
"Jack" => 10,
"Jill" => 12,
"Bob" => 14
}

如何从哈希中选择杰克和吉尔对?

student_ages.select {|key| k.length <= 4} 

1 个答案:

答案 0 :(得分:6)

student_ages.select{|key, _| key.length == 4}