-
-
Save paneq/2226107 to your computer and use it in GitHub Desktop.
Maybe in ruby
This file contains 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
def Maybe(obj) | |
Maybe.new(obj) | |
end | |
class Nothing | |
include Singleton | |
end | |
class Maybe | |
attr_reader :value | |
def initialize(value) | |
@value = value | |
end | |
def method_missing(m, *args) | |
return Maybe(Nothing.instance) if @value.nil? | |
raise NoMethodError, m unless @value.respond_to? m | |
return Maybe(@value.__send__(m)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment