Null Object Design Patternを使用したいのですが、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