Skip to content

Instantly share code, notes, and snippets.

@the-undefined
Last active February 8, 2016 11:23
Show Gist options
  • Save the-undefined/fd50c0a9cf8e838da15f to your computer and use it in GitHub Desktop.
Save the-undefined/fd50c0a9cf8e838da15f to your computer and use it in GitHub Desktop.
# Example of a guard clause performing a nil check
class BlahSpeaker
has_one :audience
def speak
return if audience.nil?
audience.address('blah ' * 100)
end
end
# Example of `NullObject` pattern
class BlahSpeaker
has_one :audience
NullAudience = Class.new do
def address(words); end
end
def speak
(audience || NullAudience.new).address('blah ' * 100)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment