生成以逗号分隔的列表

时间:2016-02-20 01:17:50

标签: ruby-on-rails ruby

我试图创建一个以逗号分隔的数据值列表,以便在图表中使用,但是我知道创建这样一个列表的方式会导致问题,如果其中一个点的值相同作为最后一点。

例如,以下方法都产生了无效的结果。

data: [<% @data.each do |point| %><%= point.value %><%= "," unless point == @data.last) %><% end %>]
data: [<% @data.each do |point| %><%= point.value %><%= "," unless point.eql?(@data.last) %><% end %>]

请注意如果点的值与最后一个点相同,则跳过逗号。

                                               vv
data: [0.069,0.174,0.034,0.112,0.598,0.043,0.0370.395,1.509,0.052,0.045,0.405,0.11,0.173,0.039,0.517,0.075,0.043,0.037]

我知道我的问题是我正在检查值是否相同,但我如何检查对象本身是否相同?

1 个答案:

答案 0 :(得分:3)

您可以尝试使用join

@data.map(&:value).join(',')