以下是清单:
[ [ "First", "A" ], [ "Second", "B" ] ]
现在我正在使用:
"#{list[0]} is the first element"
返回:
"firsta is the first element"
我希望它返回:
["First", "A"] is the first element
完整代码:
set1 = list[0].last.downcase
set2 = list[1].last.downcase
if set1.eql?(set2)
"#{list[0]} is the first element"
elseif
#to-do
else
#to-do
end
另外,我使用的是labs.codecademy.com(Ruby 1.8.7)的在线翻译。
答案 0 :(得分:3)
您正在使用Ruby 1.8,而您正在看到1.8版本的Array#to_s
:
to_s→string
返回
self.join
。[ "a", "e", "i", "o" ].to_s #=> "aeio"
使用Ruby 1.9可以获得您期望的输出:
<强> to_s()强>
别名:
inspect
但您可以在1.8:
中自己使用inspect
1.8.7 >> list = [ [ "First", "A" ], [ "Second", "B" ] ]
=> [["First", "A"], ["Second", "B"]]
1.8.7 >> list[0].to_s
=> "FirstA"
1.8.7 >> "#{list[0].inspect} is the first element"
=> "["First", "A"] is the first element"