Hacker News new | ask | show | jobs
by mcobrien 5335 days ago
I'm not sure I like it, but this works

    def nil.method_missing(*args, &block); end
So now you can call nil.whatever.you.like and get nil. It's also still falsy, but now every nil value in your app silently works and doesn't throw an error.

It's both impressive and scary that Ruby lets you (or that new contractor you're not sure about) change the fundamentals of the language in one line of code.

2 comments

I recall that this was the default action of Smalltalk? I can't find a reference though, so I am very likely incorrect.

In Objective-C nil will absorb any messages too, see the section "Sending Messages to nil" in http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/...

It isn't the default action in smalltalk. nil does not silently absorb messages in smalltalk.