Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Patterns > Using .nil?

Using .nil?

Very often I find code that uses the method nil? to determine whether a certain value is equal to nil. Writing expr.nil?, however, performs a method call in the Ruby interpreter. This is somewhat stupid of Ruby, as it is not possible to redefine nil? in any way, and the compiler could easily optimize this on the fly.

If you know that the value tested is not a Boolean, you can usually eliminate the method call. For example, you can transform


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial