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? Email me at craig@barkingiguana.com.