2015-11-03 67 views
0

第一行代码正确输出字符串。 但是第二行代码中的相同变量被[“”]包围。为什么是这样,我如何摆脱周围的引号和括号? temp_hash[0][:item]是从保持哈希散列输出变量包围在括号和引号中

puts temp_hash[0][:item] 
puts "Sku is #{a} the amount sold is #{b} the name of the book is #{temp_hash[0][:item]} and the revenue per book is #{revenue.round(2)} " 

回答

1

当你在红宝石数组的数组:arr = ["1"]并做puts arr那么输出将只是1。什么类型是puts temp_hash[0][:item]?这听起来像它是一个数组,试试这个:

puts "Sku is #{a} the amount sold is #{b} the name of the book is #{temp_hash[0][:item].first} and the revenue per book is #{revenue.round(2)} "

+0

对不起它是通过哈希 – Aaron

+0

@Aaron是数组,但它听起来像temp_hash [0] [:项]本身是一个数组,所以不是“#{temp_hash [0] [:item]}”你应该放置“#{temp_hash [0] [:item] .first}” – Macmee

+0

谢谢,ruby有点奇怪来自C++背景 – Aaron