To answer your question,
1. def author
2. author || NullUser.new
3. super
4. end
In line 1, you're defining an author
method. Then in line 2, you're again calling that author method! This keeps on happening, and you get the stack level too deep error. The proper way to do that is,
def author
super || NullUser.new
end
So you're not again calling the author method inside itself. You're just calling the super class or returning NullUser. In case you get a nil error when you're calling super
, then add an additional nil check:
def author
(super || NullUser.new) rescue NullUser.new
end
The rescue statement will catch all errors and then return NullUser.new, so you don't have to worry about super throwing an error as well.
EDIT:
Another way of handling super throwing exception which looks nicer:
def author
(super rescue nil) || NullUser.new
end