Say you have an ordered array like this, generated from a database of addresses:
[
{ city: Sacramento, state: CA },
{ city: San Francisco, state: CA },
{ city: Seattle, state: WA }
]
And you want to generate HTML with it like this:
<p>CA</p>
<ul>
<li>Sacramento</li>
<li>San Francisco</li>
</ul>
<p>WA</p>
<ul>
<li>Seattle</li>
</ul>
So you're grouping by state. One approach to doing this would be to remember the last row on each iteration of the loop and to display the state and bookending UL tags only if the current row's state is the same as the last rows state. That seems kind of nasty and non Ruby-y.
Anyone have any advice on an elegant Ruby/Rails approach to this?