Skip to content

Instantly share code, notes, and snippets.

@kmdsbng
Created April 17, 2012 03:42
Show Gist options
  • Select an option

  • Save kmdsbng/2403241 to your computer and use it in GitHub Desktop.

Select an option

Save kmdsbng/2403241 to your computer and use it in GitHub Desktop.
class Never
attr_accessor :value
instance_methods.each do |v|
undef_method(v) unless %w(object_id __id__ __send__).include?(v.to_s)
end
def method_missing(*args)
self
end
def end
nil
end
def maybe
self
end
def maybe?
true
end
end
class Maybe < Never
def initialize(value)
@value = value
end
def method_missing(*args, &block)
@value ? Maybe.new(@value.__send__(*args, &block)) : Never.new
end
def end
@value
end
end
class Object
def maybe
Maybe.new(block_given? ? yield : self)
rescue Exception
Never.new
end
def maybe?
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment