2012-03-03 50 views
-1

这里是清单:如何从两个元素列表中打印出一个元素,同时保留括号和引号?

[ [ "First", "A" ], [ "Second", "B" ] ] 

现在我使用:

"#{list[0]} is the first element" 

将返回:

"firsta is the first element" 

我想它返回:

["First", "A"] is the first element 

Full code:

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)的在线解释器。

+1

你能发布完整的程序代码吗?我在IRB中尝试这样做,它给了我期望的结果,我不确定你如何看待“firsta是第一个元素”。 – 2012-03-03 19:56:07

+2

他使用红宝石1.8我想 – fl00r 2012-03-03 20:01:39

回答

3

你使用Ruby 1.8和你看到的Array#to_s 1.8的版本:

to_s→字符串

返回self.join

[ "a", "e", "i", "o" ].to_s #=> "aeio" 

使用Ruby 1.9会给你你期待的输出:

to_s()

别名:inspect

但你可以使用自己在012中的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" 
相关问题