Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby `尝试从哈希中获取值时,未将字符串隐式转换为整数`错误_Ruby_Hash - Fatal编程技术网

Ruby `尝试从哈希中获取值时,未将字符串隐式转换为整数`错误

Ruby `尝试从哈希中获取值时,未将字符串隐式转换为整数`错误,ruby,hash,Ruby,Hash,我正试图使用它的键获取散列中的值,如下所示 #!/usr/bin/ruby $, = ", " months = Hash.new( "month" ) months = {"1" => "January", "2" => "February"} keys = months.keys["1"] puts "#{keys}" 我得到以下错误 main.rb:7:in `[]': no implicit conversion of String into Integer (Type

我正试图使用它的键获取散列中的值,如下所示

#!/usr/bin/ruby

$, = ", "
months = Hash.new( "month" )
months = {"1" => "January", "2" => "February"}

keys = months.keys["1"]
puts "#{keys}"
我得到以下错误

main.rb:7:in `[]': no implicit conversion of String into Integer (TypeError)
    from main.rb:7:in `<main>'
main.rb:7:in`[]:没有将字符串隐式转换为整数(TypeError)
从main.rb:7:in`'

为什么我会出现上述错误?

您要查找的只是

months["1"]

为什么要使用
方法?将所有键作为数组返回。因此,你可以通过数字访问它。这就是错误所说的。

months.keys
是一个由键组成的
数组,数组必须由整数下标。