[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

for .. in .. else?

Douglas Livingstone

4/21/2005 9:40:00 PM

In my erb templates, I've got this pattern quite often:

<table>
<th>Songs</th>
<% for aSong in songList %>
<td><%= aSong.name %></td>
<% end %>
<% unless songList.empty? %>
<td>no songs!</td>
<% end %>
</table>

Is there any way to write is more like this:

<table>
<th>Songs</th>
<% for aSong in songList %>
<td><%= aSong.name %></td>
<% else %>
<td>no songs!</td>
<% end %>
</table>

Thanks,
Douglas



18 Answers

dblack

4/21/2005 9:55:00 PM

0

Ryan Leavengood

4/21/2005 10:33:00 PM

0

David A. Black wrote:
> Hi --
>
> How about (untested):
>
> <table>
> <tr>
> <th>Songs</th>
> </tr>
> <% if song_list.empty? %>
> <tr>
> <td>no songs!</td>
> </tr>
> <% else %>
> <% for song in song_list %>
> <tr>
> <td><%= song.name %></td>
> </tr>
> <% end %>
> <% end %>
> </tr>
> </table>
>
> (caMelCaSe method and variable name cleansing at no extra charge :-)
>
>
> David

The above works (I tested it) but I think this is more Rubyish:

<table>
<tr>
<th>Songs</th>
</tr>
<% if song_list.empty? %>
<tr>
<td>no songs!</td>
</tr>
<% else song_list.each do |song|%>
<tr>
<td><%= song.name %></td>
</tr>
<% end %>
<% end %>
</tr>
</table>

i.e. get rid of the for entirely.

Ryan Leavengood


Hal E. Fulton

4/21/2005 10:47:00 PM

0

Ryan Leavengood wrote:
>
> The above works (I tested it) but I think this is more Rubyish:
>
> <table>
> <tr>
> <th>Songs</th>
> </tr>
> <% if song_list.empty? %>
> <tr>
> <td>no songs!</td>
> </tr>
> <% else song_list.each do |song|%>
> <tr>
> <td><%= song.name %></td>
> </tr>
> <% end %>
> <% end %>
> </tr>
> </table>
>
> i.e. get rid of the for entirely.

I don't object to the 'for' (although I know some don't like it).

FWIW I'll offer this (untested) alternative:

<table>
<tr><th>Songs</th></tr>
<% unless song_list.each do |song|%>
<tr><td><%= song.name %></td></tr>
<% end.empty? %>
<tr><td>no songs!</td></tr>
<% end %>
</tr>
</table>


Hal



Lionel Thiry

4/21/2005 11:53:00 PM

0

Douglas Livingstone a écrit :
> In my erb templates, I've got this pattern quite often:
>
> <table>
> <th>Songs</th>
> <% for aSong in songList %>
> <td><%= aSong.name %></td>
> <% end %>
> <% unless songList.empty? %>
> <td>no songs!</td>
> <% end %>
> </table>
>
> Is there any way to write is more like this:
>
> <table>
> <th>Songs</th>
> <% for aSong in songList %>
> <td><%= aSong.name %></td>
> <% else %>
> <td>no songs!</td>
> <% end %>
> </table>
>

<table>
<th>Songs</th>
<% (songList.empty? ? ["no songs!"] : songList).each do |aSong| %>
<td><%= aSong %></td>
<% end %>
</table>

--
Lionel Thiry

Personal website: http://users.skynet....

dblack

4/22/2005 12:15:00 AM

0

Adam Keys

4/22/2005 11:44:00 AM

0

On Apr 21, 2005, at 5:46 PM, Hal Fulton wrote:
> FWIW I'll offer this (untested) alternative:
>
> <table>
> <tr><th>Songs</th></tr>
> <% unless song_list.each do |song|%>
> <tr><td><%= song.name %></td></tr>
> <% end.empty? %>
> <tr><td>no songs!</td></tr>
> <% end %>
> </tr>
> </table>

A few weeks ago I was on a crusade to not create temporary Arrays and
Hashes. I wrote a bit of code something like this:

ary.reject do |item|
item.some_predicate?
end.each do |doodad|
doodad.do_something
end

I was showing it to a non-Rubyist and he thought it was a little hard
to read. I agreed it may be a little too clever for its own good.
Obviously Hal doesn't think so :). Does anyone else have strong
feelings on code like the above?

--
~akk
http://there...



dblack

4/22/2005 12:18:00 PM

0

Douglas Livingstone

4/22/2005 12:27:00 PM

0

On 4/21/05, Hal Fulton <hal9000@hypermetrics.com> wrote:
>
> FWIW I'll offer this (untested) alternative:
>
> <table>
> <tr><th>Songs</th></tr>
> <% unless song_list.each do |song|%>
> <tr><td><%= song.name %></td></tr>
> <% end.empty? %>
> <tr><td>no songs!</td></tr>
> <% end %>
> </tr>
> </table>
>
>

Now that is nice :)

Thanks all,
Douglas



Gavin Kistner

4/22/2005 12:56:00 PM

0

On Apr 22, 2005, at 6:27 AM, Douglas Livingstone wrote:
> On 4/21/05, Hal Fulton <hal9000@hypermetrics.com> wrote:
>>
>> FWIW I'll offer this (untested) alternative:
>>
>> <table>
>> <tr><th>Songs</th></tr>
>> <% unless song_list.each do |song|%>
>> <tr><td><%= song.name %></td></tr>
>> <% end.empty? %>
>> <tr><td>no songs!</td></tr>
>> <% end %>
>> </tr>
>> </table>
>>
>>
>
> Now that is nice :)

Shouldn't that be "if" instead of "unless"?



dblack

4/22/2005 1:22:00 PM

0