
It caches the results of methods that do time-consuming work, work that only needs to be done once. Memoization is a technique you can use to speed up your accessor methods. This is saying: “if 0 is true then print 123“.4 Simple Memoization Patterns in Ruby (And One Gem) Some languages consider the number 0 as a false value, but that’s not the case in Ruby: TruthinessĪnother thing you need to know about nil is that it’s the only value, besides false (which is also an object), that is considered “falsy”.Įverything else in Ruby is considered true in a boolean context. These kind of objects are great for your presentation layer, but not so much for you controllers where you want to do something different if what you are looking for is not available, like showing an alert message, redirect, etc. Tip: If you can treat all your objects the same way, without having to check object type then you are taking advantage of Polymorphism. This means that you will not get a NoMethodError exception. Now if we have an array with a mix of real product objects & null products then all of these products will have the name method.

We can return a null product like this one: In her excellent talk, “ Nothing is Something“, Sandi Metz makes the point that while nil means “nothing” it can have a more specific meaning.įor example, if we write a method that finds products & we want to return a default product…

No discussion about nil is complete without mentioning the Null Object Pattern. The reason for this is that nil is used to represent “nothing”. Notice how all these to_something methods return an empty value ( 0 for numbers). Here’s the class definition from Rubinius:

Just like any other Ruby object nil has a set of methods. The solution in this case is to simply remove the puts from inside the block. I mention this because I have seen some of my students try something like this: So watch out for that, it might just happen that you misspelt the name of your instance variable or you forgot to initialize it!Īnother place where you can find nil is with the puts & print methods. You should be aware that undefined instance variables will return nil Or you can use the “Null Object Pattern” in your own classes if you want to return a default value instead of nil. This is why Ruby 2.3 introduced the “safe navigator” operator ( &.).Īnother way to test for nil is to use the nil? method:Īnother option is to use the blank? method (Rails only).ĭepending on what object you are working with there are a few techniques you can use to avoid checking for nil values entirely.įor example, you can use the fetch method on Hash & Array objects. If you don’t want to get the “undefined method for nil:NilClass” error try this… To avoid this error, you will need to check if you have a nil value before you call a method on it. # NoMethodError: undefined method 'size' for nil:NilClass This will often lead to all sorts of problems since we expect the method we called to return a valid value instead of a nil. This happens when you ask for a value but that value is not available.

Let’s take a deeper look! Where Do Nil Values Come From? There is ONLY one nil object, with an object_id of 4 (or 8 in 64-bit Ruby), this is part of why nil is special. It’s also a “falsy” value, meaning that it behaves like false when used in a conditional statement. Well, nil is a special Ruby object used to represent an “empty” or “default” value.
