diff --git a/lib/errgonomic/option.rb b/lib/errgonomic/option.rb index 4b81bf6..3a84c8f 100644 --- a/lib/errgonomic/option.rb +++ b/lib/errgonomic/option.rb @@ -343,7 +343,22 @@ def to_json(*_args) end # filter - # xor + + # Return Some when either self or other are Some, otherwise return None + # when both are None or both are Some. + # + # @example + # Some(:left).xor(Some(:right)) # => None() + # Some(:left).xor(None()) #=> Some(:left) + # None().xor(Some(:right)) #=> Some(:right) + # + def xor(other) + return self if some? && other.none? + return other if other.some? && none? + + None() + end + # insert # get_or_insert # get_or_insert_with