Sometimes you don't need something fancy
class NullObject # You can instantiate a new NullObject with arbitrary methods by calling # Interactions.NullObject.new({method1: "outcome1"}) # That will apply the method to the object def initialize(methods={}) methods.each_pair do |k,v| define_singleton_method k do v end end end end
Then all you have to do to call it is:
NullObject.new(method1: "Output")
And the key is the method name and the value is whatever you want it to output.
def user creator || NullObject.new(name: "Jim", accounts: []) end
Will let your nullobject do user.accounts and return an empty array. Yay you always get an array instead of a NilClass error.