我只想使用空对象设计模式,但我发现我可以从 NilClass 继承。
我可以写一个方法“nil?” 并返回 false 但如果用户在下面编写代码怎么办
if null_object
puts "shouldn't be here"
end
为了澄清我试图做的是:
record = DB.find(1)
# if it can not find record 1, the bellow code should not raise exception
record.one_attr
# and what's more
if record
puts "shouldn't be here"
end
# I don't want to override all NilClass