Empty array check

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...

Michael Pavling wrote in post #957448:

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

Frederick Cheung wrote in post #957520:

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.

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

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.2

Best,