First, hats off to rails and the community. I''m definitely a convert. Question: I''m constantly outputting information like so: <% unless item.empty? %> Item: <%= item %> <% end %> But I''m finding that depending on where item is pulled from, the item will either be empty (ie form), or null (ie db). Short of always checking for nil first then empty, what''s the easiest way to just use empty and have that stop throwing errors if it''s called on a null object? Thanks, Chad --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Chad Arimura wrote:> > First, hats off to rails and the community. I’m definitely a convert. > > Question: I’m constantly outputting information like so: > > <% unless item.empty? %> > > Item: <%= item %> > > <% end %> > > But I’m finding that depending on where item is pulled from, the item > will either be empty (ie form), or null (ie db). Short of always > checking for nil first then empty, what’s the easiest way to just use > empty and have that stop throwing errors if it’s called on a null object? > > > Thanks, > > Chad > > > >Try calling blank? instead of empty? Matthew Margolis blog.mattmargolis.net --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 10/17/06, Chad Arimura <carimura-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > First, hats off to rails and the community. I''m definitely a convert. > > > > Question: I''m constantly outputting information like so: > > > > <% unless item.empty? %> > > Item: <%= item %> > > <% end %> > > > > But I''m finding that depending on where item is pulled from, the item will > either be empty (ie form), or null (ie db). Short of always checking for > nil first then empty, what''s the easiest way to just use empty and have that > stop throwing errors if it''s called on a null object? >You can have a little rescue clause in there if you like. Item: <%= item rescue "N/A" %> That should stop the errors from coming. If you search around this has been discussed a number of times. Also checkout rails.techno-weenie.net I think there are also some questions there as well. Hope that helps. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Thanks guys.. Ya I searched a bit and for some reason didn''t seem to find a definitive answer. Blank works perfectly for now! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Daniel N wrote:> On 10/17/06, Chad Arimura <carimura-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Short of always checking for > > nil first then empty, what''s the easiest way to just use empty and have that > > stop throwing errors if it''s called on a null object? > > > You can have a little rescue clause in there if you like. > > Item: <%= item rescue "N/A" %> > > That should stop the errors from coming.As well as a multitude of other possible errors that might arise from that method call. A big naked catch-all ''rescue'' like that scares me -- if something unexpected happens, you''ll get no notification of it, and it will make the bug trickier to track down. Much better to just deal with the specific situation -- in this case, check for nil and then check for empty, which blank?() does for you. Chris --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---