未定义的Ruby数组上的方法'to_h' [英] Undefined method 'to_h' on Ruby array
问题描述
根据 Ruby Array文档,有一个方法 to_h
,只要数组的每个元素是另一个包含两个元素的数组,就可以用来将数组转换为散列。下面的示例来自同一文档
p [[:foo,:bar],[1,2]]。to_h
但是,当我运行上述代码时,出现此错误:
irb(main):001:0> p [[:foo,:bar],[1,2]]。to_h
NoMethodError:[[:foo,:bar],[1,2]]:undefined方法`to_h']:Array
from(irb):1
from E:/ RubyInstall / bin / irb:12:in< main>'
irb(main):002:0>
我的Ruby版本是
C:\> ruby -v
ruby 2.0.0p247(2013-06-27)[x64-mingw32]
我想用这种技术来回答Stackoverflow上的另一个问题,但现在我被卡住了,因为Ruby的记录功能不适合我。
to_h
方法仅适用于Ruby 2.1或更高版本。
查看发行说明这里
http://pivotallabs.com/ruby-2-1-0-changes-of-note/
As per Ruby Array documentation, there is a method to_h
which can be used to convert arrays to hash as long as each element of the array is another array of two elements. Example below from the same documentation
p [[:foo, :bar], [1, 2]].to_h
However, when I run the above code, I get this error:
irb(main):001:0> p [[:foo, :bar], [1, 2]].to_h
NoMethodError: undefined method `to_h' for [[:foo, :bar], [1, 2]]:Array
from (irb):1
from E:/RubyInstall/bin/irb:12:in `<main>'
irb(main):002:0>
My Ruby version is
C:\>ruby -v
ruby 2.0.0p247 (2013-06-27) [x64-mingw32]
I wanted to use this technique to answer another question on Stackoverflow, but I am stuck now as a documented feature of Ruby is not working for me.
The to_h
method is only available in Ruby version 2.1 or higher.
Check the release notes here
http://pivotallabs.com/ruby-2-1-0-changes-of-note/
这篇关于未定义的Ruby数组上的方法'to_h'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!