[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

cleaner Ruby in a rails view

petermichaux

3/2/2006 3:59:00 AM

Hi,

I think this is a pure Ruby question. I seem to be writing this nested
structure constantly

<% unless item.cart_item_optional_variations.empty? %>
<% item.cart_item_option_values.each do |ov| %>

<% end %>
<% end %>

Is there a way to write this in one steps so i am not doing nil.each?

Thanks,
Peter

29 Answers

Scott

3/2/2006 4:06:00 AM

0

You can make sure that item.cart_item_optional_values is always equal
to an array, be it empty or full, that way you're .each wont fail, it
just wont have anything to enumerate over with an empty array.

petermichaux

3/2/2006 4:09:00 AM

0

is something like the following acceptable?


<% ( item.cart_item_option_values || [] ).each do |ov| %>

<% end %>

Pit Capitain

3/2/2006 10:37:00 AM

0

petermichaux@yahoo.com schrieb:
>
> I think this is a pure Ruby question. I seem to be writing this nested
> structure constantly
>
> <% unless item.cart_item_optional_variations.empty? %>
> <% item.cart_item_option_values.each do |ov| %>
>
> <% end %>
> <% end %>
>
> Is there a way to write this in one steps so i am not doing nil.each?

Hi Peter, since empty? isn't a method of NilClass, it seems that your
arrays never are nil, so you can just remove the empty? test.

Regards,
Pit


Christian Neukirchen

3/2/2006 11:38:00 AM

0

petermichaux@yahoo.com writes:

> Hi,
>
> I think this is a pure Ruby question. I seem to be writing this nested
> structure constantly
>
> <% unless item.cart_item_optional_variations.empty? %>
> <% item.cart_item_option_values.each do |ov| %>
>
> <% end %>
> <% end %>
>
> Is there a way to write this in one steps so i am not doing nil.each?

How about this:

<% item.cart_item_option_values.to_a.each do |ov| %>
<% end %>

# nil.to_a == []

> Thanks,
> Peter
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


dblack

3/2/2006 2:18:00 PM

0

Charlie Bowman

3/2/2006 3:03:00 PM

0

As a side thought. Why doesn't the nil class have an each method that
returns nothing. It seems that this problem comes up quite often.


On Thu, 2006-03-02 at 23:18 +0900, dblack@wobblini.net wrote:

> Hi --
>
> On Thu, 2 Mar 2006, petermichaux@yahoo.com wrote:
>
> > is something like the following acceptable?
> >
> >
> > <% ( item.cart_item_option_values || [] ).each do |ov| %>
> >
> > <% end %>
>
> If you get the right-hand side of that, it means the left-hand side
> was nil or false -- in which case calling #empty? on it will raise an
> error. (Actually in your original post you had _values once and
> _variations once, but I'm assuming that was an error.)
>
> On the other hand, if you're sure you can call #empty? on it, that
> suggests you know it will be an array, in which case you can just call
> #each on it and let it iterate zero times if it's empty.
>
>
> David


Charlie Bowman
http://www.recentr...

James Byrne

3/2/2006 3:26:00 PM

0

Charlie Bowman wrote:
> As a side thought. Why doesn't the nil class have an each method that
> returns nothing. It seems that this problem comes up quite often.
>

irb(main):001:0> a = nil
=> nil
irb(main):002:0> a.to_a
=> []
irb(main):003:0> a.to_a.each {|n| puts "The index is: #{n.index}"}
=> []
irb(main):004:0>


Does this suit?

Regards,
Jim

--
Posted via http://www.ruby-....


James Byrne

3/2/2006 3:29:00 PM

0

James Byrne wrote:

> irb(main):003:0> a.to_a.each {|n| puts "The index is: #{n.index}"}

should be:

a.to_a.each {|n| puts "The index is: #{a.to_a.index(n)}"}

sigh...


--
Posted via http://www.ruby-....


dblack

3/2/2006 3:30:00 PM

0

Charlie Bowman

3/2/2006 3:34:00 PM

0

I know this is the ruby list and not the rails list but it would be nice
if we didn't have to add code to our views to rescue nils. I guess you
could add the method each if you to the nil class right?

On Fri, 2006-03-03 at 00:29 +0900, dblack@wobblini.net wrote:

> Hi --
>
> On Fri, 3 Mar 2006, Charlie Bowman wrote:
>
> > As a side thought. Why doesn't the nil class have an each method that
> > returns nothing. It seems that this problem comes up quite often.
>
> Because it's not a container or iterator.
>
>
> David


Charlie Bowman
http://www.recentr...