在Ruby中使用范围作为键
我有一个使用范围作为键的哈希表在Ruby中使用范围作为键,ruby,range,Ruby,Range,我有一个使用范围作为键的哈希表 hash = { 1..10 => "Foo", 11..20 => "Bar", 21..30 => "Baz", 31..40 => "Quux", } hash.find {|key, value| key == 5} # => `nil` 为什么它不返回Foo 编辑: 正如下面指出的,将散列更改为散列,使用=检查是否存在真正的相等,5不是范围。但是您可以使用==或包含?。您也可以尝试选择而不是查找 例如:
hash = {
1..10 => "Foo",
11..20 => "Bar",
21..30 => "Baz",
31..40 => "Quux",
}
hash.find {|key, value| key == 5} # => `nil`
为什么它不返回Foo
编辑:
正如下面指出的,将
散列
更改为散列
,使用=
检查是否存在真正的相等,5不是范围。但是您可以使用==
或包含?
。您也可以尝试选择而不是查找
例如:
hash = {
1..10 => "Foo",
11..20 => "Bar",
21..30 => "Baz",
31..40 => "Quux",
}
p hash.find {|key, value| key === 5} #[1..10, "Foo"]
p hash.find {|key, value| key.include?(5)} #[1..10, "Foo"]
p hash.select{|key, value| key === 5} #{1..10=>"Foo"}
p hash.select{|key, value| key.include?(5)}#{1..10=>"Foo"}
请看不同的结果find
返回一个数组,`选择一个散列
结束语:您使用了Hash=…
。我希望这是一个输入错误,您希望使用散列
使用==
检查是否存在真正的相等值,5不在范围内。但是您可以使用==
或包含?
。您也可以尝试选择而不是查找
例如:
hash = {
1..10 => "Foo",
11..20 => "Bar",
21..30 => "Baz",
31..40 => "Quux",
}
p hash.find {|key, value| key === 5} #[1..10, "Foo"]
p hash.find {|key, value| key.include?(5)} #[1..10, "Foo"]
p hash.select{|key, value| key === 5} #{1..10=>"Foo"}
p hash.select{|key, value| key.include?(5)}#{1..10=>"Foo"}
请看不同的结果find
返回一个数组,`选择一个散列
结束语:您使用了Hash=…
。我希望这是一个输入错误,您希望使用散列
使用==
检查是否存在真正的相等值,5不在范围内。但是您可以使用==
或包含?
。您也可以尝试选择而不是查找
例如:
hash = {
1..10 => "Foo",
11..20 => "Bar",
21..30 => "Baz",
31..40 => "Quux",
}
p hash.find {|key, value| key === 5} #[1..10, "Foo"]
p hash.find {|key, value| key.include?(5)} #[1..10, "Foo"]
p hash.select{|key, value| key === 5} #{1..10=>"Foo"}
p hash.select{|key, value| key.include?(5)}#{1..10=>"Foo"}
请看不同的结果find
返回一个数组,`选择一个散列
结束语:您使用了Hash=…
。我希望这是一个输入错误,您希望使用散列
使用==
检查是否存在真正的相等值,5不在范围内。但是您可以使用==
或包含?
。您也可以尝试选择而不是查找
例如:
hash = {
1..10 => "Foo",
11..20 => "Bar",
21..30 => "Baz",
31..40 => "Quux",
}
p hash.find {|key, value| key === 5} #[1..10, "Foo"]
p hash.find {|key, value| key.include?(5)} #[1..10, "Foo"]
p hash.select{|key, value| key === 5} #{1..10=>"Foo"}
p hash.select{|key, value| key.include?(5)}#{1..10=>"Foo"}
请看不同的结果find
返回一个数组,`选择一个散列
结束语:您使用了Hash=…
。我希望这是一个输入错误,您希望使用散列
结构设计用于此目的时的情况
x = 5
p case x
when 1..10 then "Foo"
when 11..20 then "Bar"
when 21..30 then "Baz"
when 31..40 then "Quux"
end
# => "Foo"
设计结构时的情况
x = 5
p case x
when 1..10 then "Foo"
when 11..20 then "Bar"
when 21..30 then "Baz"
when 31..40 then "Quux"
end
# => "Foo"
设计结构时的情况
x = 5
p case x
when 1..10 then "Foo"
when 11..20 then "Bar"
when 21..30 then "Baz"
when 31..40 then "Quux"
end
# => "Foo"
设计结构时的情况
x = 5
p case x
when 1..10 then "Foo"
when 11..20 then "Bar"
when 21..30 then "Baz"
when 31..40 then "Quux"
end
# => "Foo"
对于您的特定示例,您可以使用
Hash[(k-1)/10]
例如,如果k=15
:
Hash[(15-1)/10] => Hash[1] => "Bar"
对于一般情况,如果速度很重要,首先构造另一个哈希:
H=Hash.flat_map { |r,v| r.to_a.product([v]) }.to_h
#=> { 1=>"Foo" , 2=>"Foo" ,..., 10=>"Foo",
# 11=>"Bar" , 12=>"Bar" ,..., 20=>"Bar",
# ...
# 31=>"Quux", 32=>"Quux",..., 40=>"Quux"}
因此,您可以只查找值:
H[15] #=> "Bar"
H[35] #=> "Quux"
对于您的特定示例,您可以使用
Hash[(k-1)/10]
例如,如果k=15
:
Hash[(15-1)/10] => Hash[1] => "Bar"
对于一般情况,如果速度很重要,首先构造另一个哈希:
H=Hash.flat_map { |r,v| r.to_a.product([v]) }.to_h
#=> { 1=>"Foo" , 2=>"Foo" ,..., 10=>"Foo",
# 11=>"Bar" , 12=>"Bar" ,..., 20=>"Bar",
# ...
# 31=>"Quux", 32=>"Quux",..., 40=>"Quux"}
因此,您可以只查找值:
H[15] #=> "Bar"
H[35] #=> "Quux"
对于您的特定示例,您可以使用
Hash[(k-1)/10]
例如,如果k=15
:
Hash[(15-1)/10] => Hash[1] => "Bar"
对于一般情况,如果速度很重要,首先构造另一个哈希:
H=Hash.flat_map { |r,v| r.to_a.product([v]) }.to_h
#=> { 1=>"Foo" , 2=>"Foo" ,..., 10=>"Foo",
# 11=>"Bar" , 12=>"Bar" ,..., 20=>"Bar",
# ...
# 31=>"Quux", 32=>"Quux",..., 40=>"Quux"}
因此,您可以只查找值:
H[15] #=> "Bar"
H[35] #=> "Quux"
对于您的特定示例,您可以使用
Hash[(k-1)/10]
例如,如果k=15
:
Hash[(15-1)/10] => Hash[1] => "Bar"
对于一般情况,如果速度很重要,首先构造另一个哈希:
H=Hash.flat_map { |r,v| r.to_a.product([v]) }.to_h
#=> { 1=>"Foo" , 2=>"Foo" ,..., 10=>"Foo",
# 11=>"Bar" , 12=>"Bar" ,..., 20=>"Bar",
# ...
# 31=>"Quux", 32=>"Quux",..., 40=>"Quux"}
因此,您可以只查找值:
H[15] #=> "Bar"
H[35] #=> "Quux"
因为5
不在键中,当然:)它匹配1..10
,但它不是1..10
。也可以使用hash=
。或者您将得到警告:已初始化的常量哈希
。因为5
不在键中,当然:)它匹配1..10
,但它不是1..10
。也可以使用Hash=
。或者您将得到警告:已初始化的常量哈希
。因为5
不在键中,当然:)它匹配1..10
,但它不是1..10
。也可以使用Hash=
。或者您将得到警告:已初始化的常量哈希
。因为5
不在键中,当然:)它匹配1..10
,但它不是1..10
。也可以使用Hash=
。或者您将得到警告:已初始化常量哈希
。我发现include?
方法比使用==
更清晰,这尤其令人困惑,因为在某些语言中,==
是为了更严格的平等。我发现include?
方法比使用==
要清楚得多,这尤其令人困惑,因为在某些语言中,==
是为了更严格的平等。我发现include?
方法比使用==
要清楚得多,这是特别令人困惑的,因为===
在某些语言中是为了更严格的平等。我发现include?
方法比==
的使用要清楚得多,而===
在某些语言中是为了更严格的平等。读者:作为案例陈述,当11..20
由(11..20)==7
确定时。与相同,如果范围的元素是数字,则与相同。读卡器:作为case语句,当11..20
由(11..20)==7
确定时。与相同,如果范围的元素是数字,则与相同。读卡器:作为case语句,当11..20
由(11..20)==7
确定时。与相同,如果范围的元素是数字,则与相同。读卡器:作为case语句,当11..20
由(11..20)==7
确定时。与相同,如果范围的元素为数字,则与相同。