Skip to content

Instantly share code, notes, and snippets.

@coalwater
Created May 9, 2015 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coalwater/8c0730a0911b3f871d9c to your computer and use it in GitHub Desktop.
Save coalwater/8c0730a0911b3f871d9c to your computer and use it in GitHub Desktop.
null objects pattern
class NullBook
def title
'No book exists'
end
end
class Order
def book
@book
end
end
@orders.each do |order|
order.book.title
end
@orders.each do |order|
if @order.book.nil?
'No Book exists'
else
order.book.title
end
end
class Order
def book
@book || NullBook.new
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment