Created
June 13, 2017 16:59
-
-
Save rewinfrey/7fff0f16a624f5326cdc800ba8c2bd87 to your computer and use it in GitHub Desktop.
Maybe in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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