【发布时间】:2014-07-21 01:44:28
【问题描述】:
我有一个文件(名为ter.txt),其中包含如下哈希集内容:
{"qty"=>"gfg", "unit"=>"gfg", "item"=>"xcv", "cost"=>"0.0", "salestax"=>"0.0"}
{"qty"=>"gdf", "unit"=>"g", "item"=>"gg", "cost"=>"0.0", "salestax"=>"0.0"}
我想打印哈希集的值。我尝试了以下方法,但出现undefined local variable or method 'item'的错误
file = File.open('ter.txt', 'r').map { |line| line.split("\n")[0] }
file.each do |hash|
p hash
p " #{hash[item]}"
end
【问题讨论】:
-
这个问题似乎离题了,因为它表明了对正在解决的问题的最低限度的理解。