Robert Klemme
6/11/2008 11:41:00 AM
2008/6/11 Thorsten M=FCller <thorsten@80beans.com>:
> Hi all,
>
> that's just an idea I came up with and thought worth a discussion.
>
> Would it be possible to give each an else part?
>
> For Rails programmers it is very common to do something like this:
>
> In controller search for records:
>
> @orders =3D Order. find(:all)
>
> In view:
>
> <% unless @orders.empty? then %>
> <% @orders.each do |order| %>
> <%=3D output order somehow %>
> <% end %>
> <% else %>
> output message, that there are no orders
> <% end %>
>
> If each would have it's own else which runs if the
> used array, enumerable or whatever is empty,
> that code could look much nicer:
>
> <% @orders.each do |order| %>
> <%=3D output order somehow %>
> <% else %>
> output message, that there are no orders
> <% end %>
>
> I guess, that would be true for any other Ruby code, too.
No. This does not work because your bit translates to
@orders.each do |order|
# output order somehow expression
else
end
This would mean that the "else" branch is executed for each iteration
because it is *inside* the block. This is something you clearly do
not want.
> Please give your opinions to that idea and if you think
> there would be a chance to get this implemented one day...
I'd go with Robert's suggestion:
<% @orders.each do |order| %>
<%=3D output order somehow %>
<% end.empty? and begin %>
output message, that there are no orders
<% end %>
or
<% if @orders.each do |order| %>
<%=3D output order somehow %>
<% end.empty? %>
output message, that there are no orders
<% end %>
Note that depending on the type of @orders you do not even need to
define #empty? (Array and Hash have it). If there are special types
you could do
module Enumerable
def each_checked
found =3D false
each do |item|
found =3D true
yield item
end
found
end
end
and then
<% unless @orders.each_checked do |order| %>
<%=3D output order somehow %>
<% end then %>
output message, that there are no orders
<% end %>
Note: "then" is optional.
Kind regards
robert
--=20
use.inject do |as, often| as.you_can - without end