lib/option.rb in option-0.3.1 vs lib/option.rb in option-0.4.0
- old
+ new
@@ -7,10 +7,19 @@
case that
when OptionClass then or_nil == that.or_nil
else or_nil == that
end
end
+
+ private
+
+ def assert_option(result)
+ case result
+ when OptionClass then return result
+ else raise TypeError, "Must be an Option"
+ end
+ end
end
class SomeClass < OptionClass
def initialize(value)
@@ -50,15 +59,11 @@
def map(&blk)
Option(blk.call(get))
end
def flat_map(&blk)
- result = blk.call(get)
- case result
- when OptionClass then return result
- else raise TypeError, "Must be an Option"
- end
+ assert_option(blk.call(get))
end
def fold(if_empty, &blk)
blk.call(get)
end
@@ -73,10 +78,14 @@
def inside(&blk)
blk.call(get)
self
end
+
+ def or_else(&blk)
+ self
+ end
end
class NoneClass < OptionClass
def to_a
@@ -127,9 +136,13 @@
self
end
def inside(&blk)
self
+ end
+
+ def or_else(&blk)
+ assert_option(blk.call)
end
end
None = NoneClass.new
Some = SomeClass