Class: MonadOxide::Right
Overview
‘Right’ represents an arbitrary branch of Either. Since Either is supposed to be unbiased, you can assign any bias you like to Right, though in some ecosystems (such as Haskell, where this Either is ultimately inspired from), the Right side is the more common/favorable one, but this is only out of convention.
Instance Method Summary collapse
-
#initialize(data) ⇒ Right
constructor
A new instance of Right.
-
#inspect_left(f = nil) { ... } ⇒ Either
Falls through.
-
#inspect_right(f = nil) { ... } ⇒ Either<A, B>
Applies ‘f` or the block over the data and returns the same `Right`.
-
#left? ⇒ Boolean
Identifies that this is not a ‘Left`.
-
#left_and_then(f = nil) { ... } ⇒ Either<A, B>
The Right equivalent to Left#right_and_then.
-
#map_left(f = nil) { ... } ⇒ Either<A, B>
This is a no-op for Right.
-
#map_right(f = nil) { ... } ⇒ Either<A, C>
Applies ‘f’ or the block over the data and returns a new ‘Right’ with the returned value.
-
#right? ⇒ Boolean
Identifies that this is a ‘Right`.
-
#right_and_then(f = nil) { ... } ⇒ Left<C> | Right<C>
Invokes ‘f’ or the block with the data and returns the Either returned from that.
-
#unwrap_left ⇒ A
Dangerously access the ‘Left’ data.
-
#unwrap_right ⇒ A
Dangerously access the ‘Right’ data.
Methods inherited from Either
Constructor Details
#initialize(data) ⇒ Right
Returns a new instance of Right.
18 19 20 |
# File 'lib/right.rb', line 18 def initialize(data) @data = data end |
Instance Method Details
#inspect_left(f = nil) { ... } ⇒ Either
Falls through. @see Either#inspect_left for how this is handled in the Either case, and @see Left#inspect_left for how this is handled in the Left case.
29 30 31 |
# File 'lib/right.rb', line 29 def inspect_left(f=nil, &block) self end |
#inspect_right(f = nil) { ... } ⇒ Either<A, B>
Applies ‘f` or the block over the data and returns the same `Right`. No changes are applied. This is ideal for logging. @see Either#inspect_right for a higher level understanding how this works with Eithers in general.
43 44 45 46 |
# File 'lib/right.rb', line 43 def inspect_right(f=nil, &block) (f || block).call(@data) self end |
#left? ⇒ Boolean
Identifies that this is not a ‘Left`. For counterparts:
55 56 57 |
# File 'lib/right.rb', line 55 def left?() false end |
#left_and_then(f = nil) { ... } ⇒ Either<A, B>
The Right equivalent to Left#right_and_then. This is a no-op for Right.
65 66 67 |
# File 'lib/right.rb', line 65 def left_and_then(f=nil, &block) self end |
#map_left(f = nil) { ... } ⇒ Either<A, B>
This is a no-op for Right. @see Left#map_left.
74 75 76 |
# File 'lib/right.rb', line 74 def map_left(f=nil, &block) self end |
#map_right(f = nil) { ... } ⇒ Either<A, C>
Applies ‘f’ or the block over the data and returns a new ‘Right’ with the returned value.
87 88 89 |
# File 'lib/right.rb', line 87 def map_right(f=nil, &block) Right.new((f || block).call(@data)) end |
#right? ⇒ Boolean
Identifies that this is a ‘Right`. For counterparts:
98 99 100 |
# File 'lib/right.rb', line 98 def right?() true end |
#right_and_then(f = nil) { ... } ⇒ Left<C> | Right<C>
Invokes ‘f’ or the block with the data and returns the Either returned from that. The return type is enforced.
112 113 114 115 116 117 118 119 |
# File 'lib/right.rb', line 112 def right_and_then(f=nil, &block) either = (f || block).call(@data) if either.is_a?(Either) either else raise EitherReturnExpectedError.new(either) end end |
#unwrap_left ⇒ A
Dangerously access the ‘Left’ data. Since this is a ‘Right’, it will always raise an error. @see Either#unwrap_left. It is recommended to use this for tests only.
126 127 128 129 130 131 132 133 |
# File 'lib/right.rb', line 126 def unwrap_left() raise UnwrapError.new( " \#{self.class()} with \#{@data.inspect()} could not be unwrapped as a\n Left.\n EOE\n )\nend\n" |
#unwrap_right ⇒ A
Dangerously access the ‘Right’ data. If this is a ‘Left’, an ‘UnwrapError` will be raised. It is recommended to use this for tests only.
140 141 142 |
# File 'lib/right.rb', line 140 def unwrap_right() @data end |