Which of the two methods below are the most stable? Does it matter? !@articles.empty? @articles.length > 0 I personally prefer using the ''empty?'' method. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 27 October 2010 11:43, Pale Horse <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Which of the two methods below are the most stable? Does it matter? > > !@articles.empty? > @articles.length > 0 > > I personally prefer using the ''empty?'' method.Between the two, I''d use ".empty?", but at a choice I''d use neither, and go for ".blank?" instead. ".blank?" can be a bit more "stable" as it returns true for nil values, as well as blank strings, empty arrays and hashes... -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling wrote in post #957448:> On 27 October 2010 11:43, Pale Horse <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> Which of the two methods below are the most stable? Does it matter? >> >> !@articles.empty? >> @articles.length > 0 >> >> I personally prefer using the ''empty?'' method. > > Between the two, I''d use ".empty?", but at a choice I''d use neither, > and go for ".blank?" instead. > ".blank?" can be a bit more "stable" as it returns true for nil > values, as well as blank strings, empty arrays and hashes...Interesting. Thanks for your response. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Oct 27, 11:43 am, Pale Horse <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Which of the two methods below are the most stable? Does it matter? > > !...@articles.empty? > @articles.length > 0 > > I personally prefer using the ''empty?'' method. >I''d use .any? over !foo.empty? Fred> -- > Posted viahttp://www.ruby-forum.com/.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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?hl=en.
Frederick Cheung wrote in post #957520:> > On Oct 27, 11:43am, Pale Horse <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> Which of the two methods below are the most stable? Does it matter? >> >> !...@articles.empty? >> @articles.length > 0 >> >> I personally prefer using the ''empty?'' method. >> > > I''d use .any? over !foo.empty? > > FredI''ve not seen much documentation for it or usage of it in applications I''ve worked on but I''ll look into that. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
There is a abstraction level difference between those methods. using [].empty? or [].any? would do a Ruby call. It means that you are directly asking Ruby interpreter for a answer. That would perform better on a bigger scale. using [].blank? or [].present? (respectively) would do a Ruby call through Rails''s method. RoR implements it by using the Ruby empty? method. Which of them would be more stable? empty? and any? are faster but doesn''t work (throw error) with all instances of Object class. present? is calling the blank? method, and blank? is calling the empty? method, but never throws an error (as empty? would do for some objects) # File activesupport/lib/active_support/core_ext/object/blank.rb, line 12 def blank? respond_to?(:empty?) ? empty? : !self end I recommand using RoR methods in RoR apps. Cheers! ps. all checked for Rails 3.0.0 and Ruby 1.9.2 On 28 Paź, 10:51, Pale Horse <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frederick Cheung wrote in post #957520: > > > > > On Oct 27, 11:43am, Pale Horse <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > >> Which of the two methods below are the most stable? Does it matter? > > >> !...@articles.empty? > >> @articles.length > 0 > > >> I personally prefer using the ''empty?'' method. > > > I''d use .any? over !foo.empty? > > > Fred > > I''ve not seen much documentation for it or usage of it in applications > I''ve worked on but I''ll look into that. > > -- > Posted viahttp://www.ruby-forum.com/.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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?hl=en.
Jakub Godawa wrote in post #957822:> There is a abstraction level difference between those methods.Not in this case. Rails'' methods here are *supplements* to Ruby''s methods, not abstractions IMHO.> > using [].empty? or [].any? would do a Ruby call. It means that you are > directly asking Ruby interpreter for a answer. That would perform > better on a bigger scale. > > using [].blank? or [].present? (respectively) would do a Ruby call > through Rails''s method. RoR implements it by using the Ruby empty? > method. > > Which of them would be more stable? > empty? and any? are faster but doesn''t work (throw error) with all > instances of Object class. > present? is calling the blank? method, and blank? is calling the > empty? method, but never throws an error (as empty? would do for some > objects) > > # File activesupport/lib/active_support/core_ext/object/blank.rb, line > 12 > def blank? > respond_to?(:empty?) ? empty? : !self > end > > I recommand using RoR methods in RoR apps.Why? If Rails provides useful magic, use it, but don''t use it just because it comes from Rails. (For the record, I love blank?.)> > Cheers! > > ps. all checked for Rails 3.0.0 and Ruby 1.9.2Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.