3 ms·
To elaborate on the other answers: "nil" in Ruby is an instance of NilClass. NilClass has a few methods, but e.g. "thumbnail" is not amongst them, so Ruby look
by vidarh 11y ago
To elaborate on the other answers:
"nil" in Ruby is an instance of NilClass. NilClass has a few methods, but e.g. "thumbnail" is not amongst them, so Ruby looks for a "method_missing" method, doesn't find that either, and then throws an exception, just as if you'd called another non-existent method.
To answer a question you didn't ask, though, you can easily write classes to wrap your objects to get pretty much the effect you are asking about. E.g.:
class Maybe
attr_reader :value
def initialize value
@value = value
end
def method_missing *args
return self if @value.nil?
Maybe.new(@value.send(*args))
end
end
p Maybe.new(nil).i.can.call.anything.without.exceptions
p Maybe.new("Hello foo").gsub("foo","world").value
The first call to "p" will print a respresentation of a "Maybe" instance holding "nil" in "@value".
The second will print "Hello world", as "gsub" will be called on "Hello foo" since the "Maybe" has a non-nil value.
There are any number of implementations of helper classes like this for Ruby, with various levels of popularity. The benefit of course is that you can easily customize the behaviour for your specific needs (e.g. you could create one that will return a default value instead of nil, or you can create one that short-circuits on other values than nil, or any number of alternatives)
The advantage of introducing .? as proposed, though, is that it is easier to optimize (no new class or lots of extra objects to create and destroy).
- gotchange 11y agoSo, "nil" is not a primitive value in Ruby explains the extra care taken with these control flow statements in the language. Let's get back to the example, let's say that checking for «u» would return «nil», wouldn't checking for «u.profiles» would result in an exception thrown nonetheless?
- spoiler 11y ago> So, "nil" is not a primitive value in Ruby Almost nothing is a primitive value in Ruby. Not even true/false/numbers are "traditional" primitive values. > Let's get back to the example, let's say that checking for «u» would return «nil», wouldn't checking for «u.profiles» would result in an exception thrown nonetheless? No, because `u.?profile` would return `nil`, then, when you try `nil.?thumbnail` on nil again, it would return nil. So, doing the following, with `u` being `nil` is legal: if u.?profile.?thumbnail # doesn't run if u is nil end
- vidarh 11y agoEverything is an object in Ruby. E.g. you can do "42.to_s(16)" to convert 42 to base 16. That includes nil, true, false (instances of NilClass, TrueClass and FalseClass respectively). A common way of explicitly checking for nil, as opposed to nil or false is to do "foo.nil?", for example, which explicitly calls the method nil? on foo, which will usually return false for objects other than nil (nothing stops you from overriding it if you want your class to pretend to be nil - e.g. maybe I'd want my "Maybe" class to return true for "nil?" if the value it holds is nil). As for the example the whole point is that like my naive "Maybe" implementation, this new operator would check if the target is nil before trying to call a method. Unlike my naive implementation, it doesn't need to wrap objects, and it can short-circuit evaluation if it meets a nil (you can implement versions that will short-circuit evaluation too, but I can't see any obvious ways of both letting you short-circuit and getting "clean" syntax if you want to support method arguments).