Skip to content

Instantly share code, notes, and snippets.

@paneq
Forked from pzol/maybe.rb
Created March 28, 2012 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paneq/2226107 to your computer and use it in GitHub Desktop.
Save paneq/2226107 to your computer and use it in GitHub Desktop.
Maybe in ruby
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