Skip to content

Instantly share code, notes, and snippets.

@alex-lairan
Created April 3, 2020 12:24
Show Gist options
  • Save alex-lairan/f957c52de0ee88d73f5df8bd06dac49e to your computer and use it in GitHub Desktop.
Save alex-lairan/f957c52de0ee88d73f5df8bd06dac49e to your computer and use it in GitHub Desktop.
MaybeHash
class Some
def initialize(value)
@value = value
end
def value
@value
end
end
class None
def initialize
end
def value
raise "error"
end
end
class MaybeHash
def initialize(hash)
@hash = hash
end
def [](key)
if @hash.has_key?(key)
Some.new(@hash[key])
else
None.new()
end
end
end
hash = {
key: :value
}
hash = MaybeHash.new(hash)
pp hash[:key]
pp hash[:unknown_key]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment