从Ruby散列中的对应键检索值

时间:2022-11-25 21:17:07

The following only prints white lines. If this isn't the way to retrieve values from keys in Ruby, what is?

以下仅打印白线。如果这不是从Ruby中的键检索值的方法,那么什么是呢?

numbers = []

for i in 1..100 do
    hash = {
        :FizzBuzz => 1,
        :Prime => 3,
        :Fibonacci => 5
    }
    numbers << { i => hash }
end

numbers.each do |number|
    puts number[:Prime]
end

Note this is a MCVE, in the final application 1, 3 and 5 will be function calls.

注意,这是一个MCVE,在最终的应用程序1、3和5中将是函数调用。

Try it

试一试

For those wondering what I was trying to do, the final (non-MCVE) result can be found on Code Review.

对于那些想知道我要做什么的人,可以在代码评审中找到最终的(非mcve)结果。

3 个解决方案

#1


4  

After the first loop, numbers is an array like this:

在第一个循环之后,数字是这样的数组:

[
  { 1 => { :FizzBuzz => 1, :Prime => 3, :Fibonacci => 5 } },
  { 2 => { :FizzBuzz => 1, :Prime => 3, :Fibonacci => 5 } },
  ...
  { 100 => { :FizzBuzz => 1, :Prime => 3, :Fibonacci => 5 } }
]

number[:Prime] is trying to fetch the :Prime element out of your first array element, but you only have the key 1.

number[:Prime]试图从第一个数组元素中获取:Prime元素,但您只有键1。

The end result is, you print empty lines, as nothing is found at each iteration. Hash access is okay; it is the logic of your code that is the problem (and you did not explain what you are trying to do precisely).

最终的结果是,您打印空行,因为在每次迭代中没有发现任何内容。散列访问是好的;问题出在代码的逻辑上(而且您没有准确地解释您要做什么)。

#2


4  

Each number is a hash of this form:

每个数字都是这种形式的散列:

{1=>{:FizzBuzz=>1, :Prime=>3, :Fibonacci=>5}}

which has a number as the sole key. When you look for a hash key that does not exist using Hash#[], Ruby returns nil. And puts nil prints a blank line.

它有一个数字作为唯一的钥匙。当您使用hash #[]查找不存在的散列键时,Ruby返回nil。put nil打印空行。

Indeed Hash#[] is the way to retrieve values from a hash.

实际上,散列#[]是从散列中检索值的方法。

#3


2  

The keys of number are all integers (coming from i). The symbol :Prime is not a key. Therefore the results are all nil.

数字键都是整数(来自i)。符号:Prime不是键。因此结果都是nil。

#1


4  

After the first loop, numbers is an array like this:

在第一个循环之后,数字是这样的数组:

[
  { 1 => { :FizzBuzz => 1, :Prime => 3, :Fibonacci => 5 } },
  { 2 => { :FizzBuzz => 1, :Prime => 3, :Fibonacci => 5 } },
  ...
  { 100 => { :FizzBuzz => 1, :Prime => 3, :Fibonacci => 5 } }
]

number[:Prime] is trying to fetch the :Prime element out of your first array element, but you only have the key 1.

number[:Prime]试图从第一个数组元素中获取:Prime元素,但您只有键1。

The end result is, you print empty lines, as nothing is found at each iteration. Hash access is okay; it is the logic of your code that is the problem (and you did not explain what you are trying to do precisely).

最终的结果是,您打印空行,因为在每次迭代中没有发现任何内容。散列访问是好的;问题出在代码的逻辑上(而且您没有准确地解释您要做什么)。

#2


4  

Each number is a hash of this form:

每个数字都是这种形式的散列:

{1=>{:FizzBuzz=>1, :Prime=>3, :Fibonacci=>5}}

which has a number as the sole key. When you look for a hash key that does not exist using Hash#[], Ruby returns nil. And puts nil prints a blank line.

它有一个数字作为唯一的钥匙。当您使用hash #[]查找不存在的散列键时,Ruby返回nil。put nil打印空行。

Indeed Hash#[] is the way to retrieve values from a hash.

实际上,散列#[]是从散列中检索值的方法。

#3


2  

The keys of number are all integers (coming from i). The symbol :Prime is not a key. Therefore the results are all nil.

数字键都是整数(来自i)。符号:Prime不是键。因此结果都是nil。