lib/rumonade/option.rb in rumonade-0.1.2 vs lib/rumonade/option.rb in rumonade-0.2.0

- old
+ new

@@ -33,35 +33,37 @@ # puts name_opt.get.strip.upcase # when None # puts "No name value" # end # + # @abstract class Option class << self - # Returns a new Option containing the given value + # @return [Option] Returns an +Option+ containing the given value def unit(value) Rumonade.Option(value) end - # Returns the empty Option (None) + # @return [Option] Returns the empty +Option+ def empty None end end - def initialize # :nodoc: + def initialize raise(TypeError, "class Option is abstract; cannot be instantiated") if self.class == Option end + private :initialize # Returns None if None, or the result of executing the given block or lambda on the contents if Some def bind(lam = nil, &blk) empty? ? self : (lam || blk).call(value) end include Monad - # Returns +true+ if None, +false+ if Some + # @return [Boolean] Returns +true+ if +None+, +false+ if +Some+ def empty? raise(NotImplementedError) end # Returns contents if Some, or raises NoSuchElementError if None @@ -87,10 +89,11 @@ @value = value end attr_reader :value # :nodoc: + # @return (see Option#empty?) def empty? false end def ==(other) @@ -104,10 +107,11 @@ # Represents an Option which is empty, accessed via the constant None class NoneClass < Option include Singleton + # @return (see Option#empty?) def empty? true end def ==(other) @@ -117,18 +121,15 @@ def to_s "None" end end - # Exception raised on attempts to access the value of None - class NoSuchElementError < RuntimeError; end - # Returns an Option wrapping the given value: Some if non-nil, None if nil def Option(value) value.nil? ? None : Some(value) end - # Returns a Some wrapping the given value, for convenience + # @return [Some] Returns a +Some+ wrapping the given value, for convenience def Some(value) Some.new(value) end # The single global instance of NoneClass, representing the empty Option