Skip to content

Instantly share code, notes, and snippets.

@rewinfrey
Created June 13, 2017 16:59
Show Gist options
  • Save rewinfrey/7fff0f16a624f5326cdc800ba8c2bd87 to your computer and use it in GitHub Desktop.
Save rewinfrey/7fff0f16a624f5326cdc800ba8c2bd87 to your computer and use it in GitHub Desktop.
Maybe in Ruby
class Maybe
attr_reader :run_maybe
def initialize(result)
@run_maybe = construct(result)
end
def construct(result)
if result == ""
Nothing.new
else
Just.new(result)
end
end
class Nothing
end
class Just
attr_reader :value
def initialize(value)
@value = value
end
end
end
a = Maybe.new("")
a.run_maybe # => #<Maybe::Nothing:0x007f883b899728>
b = Maybe.new("jolo")
b.run_maybe # => #<Maybe::Just:0x007f883b899278 @value="jolo">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment