Module: Fear::Option

Included in:
None, Some
Defined in:
lib/fear/option.rb

Overview

Represents optional values. Instances of Option are either an instance of Some or the object None.

This allows for sophisticated chaining of Option values without having to check for the existence of a value.

Examples:

The most idiomatic way to use an Option instance is to treat it as a collection

name = Option(params[:name])
upper = name.map(&:strip).select { |n| n.length != 0 }.map(&:upcase)
puts upper.get_or_else('')

A less-idiomatic way to use Option values is via pattern matching

name = Option(params[:name])
case name
when Some
  puts name.strip.upcase
when None
  puts 'No name value'
end

or manually checking for non emptiness

name = Option(params[:name])
if name.empty?
  puts 'No name value'
else
  puts name.strip.upcase
end

See Also:

Defined Under Namespace

Modules: Mixin

Instance Method Summary collapse

Instance Method Details

#any? {|value| ... } ⇒ Boolean

Returns false if None or returns the result of the application of the given predicate to the Some value.

Examples:

Some(12).any?( |v| v > 10)  #=> true
Some(7).any?( |v| v > 10)   #=> false
None().any?( |v| v > 10)    #=> false

Yield Parameters:

  • value (any)

Yield Returns:

  • (Boolean)

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fear/option.rb', line 142

module Option
  # @private
  def left_class
    None
  end

  # @private
  def right_class
    Some
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Option::Mixin
  #
  #   Option(17)  #=> #<Fear::Some value=17>
  #   Option(nil) #=> #<Fear::None>
  #   Some(17)    #=> #<Fear::Some value=17>
  #   None()      #=> #<Fear::None>
  #
  module Mixin
    # An +Option+ factory which creates +Some+ if the argument is
    # not +nil+, and +None+ if it is +nil+.
    # @param value [any]
    # @return [Some, None]
    #
    def Option(value)
      if value.nil?
        None()
      else
        Some(value)
      end
    end

    # @return [None]
    def None
      None.new
    end

    # @param value [any] except nil
    # @return [None]
    def Some(value)
      Some.new(value)
    end
  end
end

#each {|value| ... } ⇒ Option

Performs the given block if this is a Some.

Examples:

Some(17).each do |value|
  puts value
end #=> prints 17

None().each do |value|
  puts value
end #=> does nothing

Yield Parameters:

  • value (any)

Yield Returns:

  • (void)

Returns:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fear/option.rb', line 142

module Option
  # @private
  def left_class
    None
  end

  # @private
  def right_class
    Some
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Option::Mixin
  #
  #   Option(17)  #=> #<Fear::Some value=17>
  #   Option(nil) #=> #<Fear::None>
  #   Some(17)    #=> #<Fear::Some value=17>
  #   None()      #=> #<Fear::None>
  #
  module Mixin
    # An +Option+ factory which creates +Some+ if the argument is
    # not +nil+, and +None+ if it is +nil+.
    # @param value [any]
    # @return [Some, None]
    #
    def Option(value)
      if value.nil?
        None()
      else
        Some(value)
      end
    end

    # @return [None]
    def None
      None.new
    end

    # @param value [any] except nil
    # @return [None]
    def Some(value)
      Some.new(value)
    end
  end
end

#empty?Boolean

Returns true if the Option is None, false otherwise.

Examples:

Some(42).empty? #=> false
None().empty?   #=> true

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fear/option.rb', line 142

module Option
  # @private
  def left_class
    None
  end

  # @private
  def right_class
    Some
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Option::Mixin
  #
  #   Option(17)  #=> #<Fear::Some value=17>
  #   Option(nil) #=> #<Fear::None>
  #   Some(17)    #=> #<Fear::Some value=17>
  #   None()      #=> #<Fear::None>
  #
  module Mixin
    # An +Option+ factory which creates +Some+ if the argument is
    # not +nil+, and +None+ if it is +nil+.
    # @param value [any]
    # @return [Some, None]
    #
    def Option(value)
      if value.nil?
        None()
      else
        Some(value)
      end
    end

    # @return [None]
    def None
      None.new
    end

    # @param value [any] except nil
    # @return [None]
    def Some(value)
      Some.new(value)
    end
  end
end

#flat_map {|value| ... } ⇒ Option

Returns the given block applied to the value from this Some or returns this if this is a None

Examples:

Some(42).flat_map { |v| Some(v/2) }   #=> Some(21)
None().flat_map { |v| Some(v/2) }     #=> None()

Yield Parameters:

  • value (any)

Yield Returns:

Returns:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fear/option.rb', line 142

module Option
  # @private
  def left_class
    None
  end

  # @private
  def right_class
    Some
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Option::Mixin
  #
  #   Option(17)  #=> #<Fear::Some value=17>
  #   Option(nil) #=> #<Fear::None>
  #   Some(17)    #=> #<Fear::Some value=17>
  #   None()      #=> #<Fear::None>
  #
  module Mixin
    # An +Option+ factory which creates +Some+ if the argument is
    # not +nil+, and +None+ if it is +nil+.
    # @param value [any]
    # @return [Some, None]
    #
    def Option(value)
      if value.nil?
        None()
      else
        Some(value)
      end
    end

    # @return [None]
    def None
      None.new
    end

    # @param value [any] except nil
    # @return [None]
    def Some(value)
      Some.new(value)
    end
  end
end

#getany

Returns the Option‘s value.

Returns:

  • (any)

    the Option‘s value.

Raises:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fear/option.rb', line 142

module Option
  # @private
  def left_class
    None
  end

  # @private
  def right_class
    Some
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Option::Mixin
  #
  #   Option(17)  #=> #<Fear::Some value=17>
  #   Option(nil) #=> #<Fear::None>
  #   Some(17)    #=> #<Fear::Some value=17>
  #   None()      #=> #<Fear::None>
  #
  module Mixin
    # An +Option+ factory which creates +Some+ if the argument is
    # not +nil+, and +None+ if it is +nil+.
    # @param value [any]
    # @return [Some, None]
    #
    def Option(value)
      if value.nil?
        None()
      else
        Some(value)
      end
    end

    # @return [None]
    def None
      None.new
    end

    # @param value [any] except nil
    # @return [None]
    def Some(value)
      Some.new(value)
    end
  end
end

#get_or_else(&default) ⇒ any #get_or_else(default) ⇒ any

Returns the value from this Some or evaluates the given default argument if this is a None.

Overloads:

  • #get_or_else(&default) ⇒ any

    Examples:

    Some(42).get_or_else { 24/2 } #=> 42
    None().get_or_else { 24/2 }   #=> 12
    

    Yield Returns:

    • (any)

    Returns:

    • (any)
  • #get_or_else(default) ⇒ any

    Examples:

    Some(42).get_or_else(12)  #=> 42
    None().get_or_else(12)    #=> 12
    

    Returns:

    • (any)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fear/option.rb', line 142

module Option
  # @private
  def left_class
    None
  end

  # @private
  def right_class
    Some
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Option::Mixin
  #
  #   Option(17)  #=> #<Fear::Some value=17>
  #   Option(nil) #=> #<Fear::None>
  #   Some(17)    #=> #<Fear::Some value=17>
  #   None()      #=> #<Fear::None>
  #
  module Mixin
    # An +Option+ factory which creates +Some+ if the argument is
    # not +nil+, and +None+ if it is +nil+.
    # @param value [any]
    # @return [Some, None]
    #
    def Option(value)
      if value.nil?
        None()
      else
        Some(value)
      end
    end

    # @return [None]
    def None
      None.new
    end

    # @param value [any] except nil
    # @return [None]
    def Some(value)
      Some.new(value)
    end
  end
end

#include?(other_value) ⇒ Boolean

Returns true if it has an element that is equal (as determined by ==) to other_value, false otherwise.

Examples:

Some(17).include?(17) #=> true
Some(17).include?(7)  #=> false
None().include?(17)   #=> false

Parameters:

  • (any)

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fear/option.rb', line 142

module Option
  # @private
  def left_class
    None
  end

  # @private
  def right_class
    Some
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Option::Mixin
  #
  #   Option(17)  #=> #<Fear::Some value=17>
  #   Option(nil) #=> #<Fear::None>
  #   Some(17)    #=> #<Fear::Some value=17>
  #   None()      #=> #<Fear::None>
  #
  module Mixin
    # An +Option+ factory which creates +Some+ if the argument is
    # not +nil+, and +None+ if it is +nil+.
    # @param value [any]
    # @return [Some, None]
    #
    def Option(value)
      if value.nil?
        None()
      else
        Some(value)
      end
    end

    # @return [None]
    def None
      None.new
    end

    # @param value [any] except nil
    # @return [None]
    def Some(value)
      Some.new(value)
    end
  end
end

#map {|value| ... } ⇒ Object

Maps the given block to the value from this Some or returns this if this is a None

Examples:

Some(42).map { |v| v/2 } #=> Some(21)
None().map { |v| v/2 }   #=> None()

Yield Parameters:

  • value (any)

Yield Returns:

  • (any)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fear/option.rb', line 142

module Option
  # @private
  def left_class
    None
  end

  # @private
  def right_class
    Some
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Option::Mixin
  #
  #   Option(17)  #=> #<Fear::Some value=17>
  #   Option(nil) #=> #<Fear::None>
  #   Some(17)    #=> #<Fear::Some value=17>
  #   None()      #=> #<Fear::None>
  #
  module Mixin
    # An +Option+ factory which creates +Some+ if the argument is
    # not +nil+, and +None+ if it is +nil+.
    # @param value [any]
    # @return [Some, None]
    #
    def Option(value)
      if value.nil?
        None()
      else
        Some(value)
      end
    end

    # @return [None]
    def None
      None.new
    end

    # @param value [any] except nil
    # @return [None]
    def Some(value)
      Some.new(value)
    end
  end
end

#reject {|value| ... } ⇒ Option

Returns Some if applying the predicate to this Option‘s value returns false. Otherwise, return None.

Examples:

Some(42).reject { |v| v > 40 } #=> None
Some(42).reject { |v| v < 40 } #=> Some(42)
None().reject { |v| v < 40 }   #=> None

Yield Parameters:

  • value (any)

Yield Returns:

  • (Boolean)

Returns:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fear/option.rb', line 142

module Option
  # @private
  def left_class
    None
  end

  # @private
  def right_class
    Some
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Option::Mixin
  #
  #   Option(17)  #=> #<Fear::Some value=17>
  #   Option(nil) #=> #<Fear::None>
  #   Some(17)    #=> #<Fear::Some value=17>
  #   None()      #=> #<Fear::None>
  #
  module Mixin
    # An +Option+ factory which creates +Some+ if the argument is
    # not +nil+, and +None+ if it is +nil+.
    # @param value [any]
    # @return [Some, None]
    #
    def Option(value)
      if value.nil?
        None()
      else
        Some(value)
      end
    end

    # @return [None]
    def None
      None.new
    end

    # @param value [any] except nil
    # @return [None]
    def Some(value)
      Some.new(value)
    end
  end
end

#select {|value| ... } ⇒ Option

Returns self if it is nonempty and applying the predicate to this Option‘s value returns true. Otherwise, return None.

Examples:

Some(42).select { |v| v > 40 } #=> Success(21)
Some(42).select { |v| v < 40 } #=> None()
None().select { |v| v < 40 }   #=> None()

Yield Parameters:

  • value (any)

Yield Returns:

  • (Boolean)

Returns:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fear/option.rb', line 142

module Option
  # @private
  def left_class
    None
  end

  # @private
  def right_class
    Some
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Option::Mixin
  #
  #   Option(17)  #=> #<Fear::Some value=17>
  #   Option(nil) #=> #<Fear::None>
  #   Some(17)    #=> #<Fear::Some value=17>
  #   None()      #=> #<Fear::None>
  #
  module Mixin
    # An +Option+ factory which creates +Some+ if the argument is
    # not +nil+, and +None+ if it is +nil+.
    # @param value [any]
    # @return [Some, None]
    #
    def Option(value)
      if value.nil?
        None()
      else
        Some(value)
      end
    end

    # @return [None]
    def None
      None.new
    end

    # @param value [any] except nil
    # @return [None]
    def Some(value)
      Some.new(value)
    end
  end
end

#to_aArray

Returns an Array containing the Some value or an empty Array if this is a None

Examples:

Some(42).to_a #=> [21]
None().to_a   #=> []

Returns:

  • (Array)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fear/option.rb', line 142

module Option
  # @private
  def left_class
    None
  end

  # @private
  def right_class
    Some
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Option::Mixin
  #
  #   Option(17)  #=> #<Fear::Some value=17>
  #   Option(nil) #=> #<Fear::None>
  #   Some(17)    #=> #<Fear::Some value=17>
  #   None()      #=> #<Fear::None>
  #
  module Mixin
    # An +Option+ factory which creates +Some+ if the argument is
    # not +nil+, and +None+ if it is +nil+.
    # @param value [any]
    # @return [Some, None]
    #
    def Option(value)
      if value.nil?
        None()
      else
        Some(value)
      end
    end

    # @return [None]
    def None
      None.new
    end

    # @param value [any] except nil
    # @return [None]
    def Some(value)
      Some.new(value)
    end
  end
end