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 <[email protected]> wrote:
> Frederick Cheung wrote in post #957520:
>
>
>
> > On Oct 27, 11:43am, Pale Horse <[email protected]> wrote:
>
> >> Which of the two methods below are the most stable? Does it matter?
>
> >> [email protected]?
> >> @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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.