Some of my pet peeves of center around arrays. Ruby provides a really rich language around arrays, use it to make your code more readable.

Ask an array if it's empty. Don't check if it has a size equal to zero.

bookmarks.size == 0 # no!
bookmarks.empty? # yes

Ask an array it it has any elements. Don't check if it has a non-zero size.

bookmarks.size > 0 # no!
bookmarks.any? # yes

You don't need to ask if an array has elements before iterating over it using each, each will not yield unless the array has elements.

if bookmarks.any?; bookmarks.each { ... }; end # pointless
bookmarks.each { ... } # does the same thing
I'm sure you have other similar peeves. Let me know what they are in the comments.
written by
Craig
published
2011-03-14
Disagree? Found a typo? Got a question?
If you'd like to have a conversation about this post, email craig@barkingiguana.com. I don't bite.
You can verify that I've written this post by following the verification instructions:
curl -LO http://barkingiguana.com/2011/03/14/be-cool-with-arrays.html.orig
curl -LO http://barkingiguana.com/2011/03/14/be-cool-with-arrays.html.orig.asc
gpg --verify be-cool-with-arrays.html.orig{.asc,}