From c055bb3e80ba780a0aecf02aac8f65774ce83f42 Mon Sep 17 00:00:00 2001 From: Nick Zadrozny Date: Tue, 28 Apr 2026 13:12:38 -0500 Subject: [PATCH] Option#xor --- lib/errgonomic/option.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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