I have an array @cities = ["Vienna", "Barcelona", "Paris"];
and I am trying to display the individual items with a spacer in between. However it is possible that there is only 1 element in the array, in which case I do not want to display the spacer. And also the array could be empty, in which case I want to display nothing.
For the above array I want the following output:
Vienna
-----
Barcelona
-----
Paris
I use an erb template cityview to apply formatting, css, etc before actually printing the city names. Simplified, it looks like this:
<p><%= @cities[@city_id] %></p>
I have implemented it as follows…
unless @array.empty?
@city_id = 0;
erb :cityview
end
unless @array[1..-1].nil?
@array[1..-1].each_index do |i|
@city_id = i+1;
puts "<p>-------</p>";
erb :cityview
end
end
Is there a better way?
I’d prefer:
erb:
and loop
I assume you have split the erb, bit because you want to customize it.