Class: Rumonade::Option Abstract
- Inherits:
-
Object
- Object
- Rumonade::Option
- Includes:
- Monad
- Defined in:
- lib/rumonade/option.rb
Overview
Represents optional values. Instances of Option are either an instance of Some or the object None.
The most idiomatic way to use an Option instance is to treat it as a collection or monad and use map, flat_map, select, or each:
name = Option(params[:name])
upper = name.map(&:strip).select { |s| s.length != 0 }.map(&:upcase)
puts upper.get_or_else("")
Note that this is equivalent to
# TODO: IMPLEMENT FOR COMPREHENSIONS
# see http://stackoverflow.com/questions/1052476/can-someone-explain-scalas-yield
val upper = for {
name <- Option(params[:name])
trimmed <- Some(name.strip)
upper <- Some(trimmed.upcase) if trimmed.length != 0
} yield upper
puts upper.get_or_else("")
Because of how for comprehension works, if None is returned from params#[], the entire expression results in None This allows for sophisticated chaining of Option values without having to check for the existence of a value.
A less-idiomatic way to use Option values is via direct comparison:
name_opt = params[:name]
case name_opt
when Some
puts name_opt.get.strip.upcase
when None
puts "No name value"
end
Constant Summary
Constants included from Monad
Monad::DEFAULT_METHODS_TO_REPLACE_WITH_MONAD
Class Method Summary collapse
-
.empty ⇒ Option
Returns the empty
Option
. -
.unit(value) ⇒ Option
Returns an
Option
containing the given value.
Instance Method Summary collapse
-
#bind(lam = nil, &blk) ⇒ Object
Returns None if None, or the result of executing the given block or lambda on the contents if Some.
-
#empty? ⇒ Boolean
Returns
true
ifNone
,false
ifSome
. -
#get ⇒ Object
Returns contents if Some, or raises NoSuchElementError if None.
-
#get_or_else(val_or_lam = nil, &blk) ⇒ Object
Returns contents if Some, or given value or result of given block or lambda if None.
-
#or_nil ⇒ Object
Returns contents if Some, or
nil
if None.
Methods included from Monad
#can_flatten_in_monad?, #each, #flat_map_with_monad, #flatten_with_monad, included, #map_with_monad, #select, #shallow_flatten
Class Method Details
Instance Method Details
#bind(lam = nil, &blk) ⇒ Object
Returns None if None, or the result of executing the given block or lambda on the contents if Some
58 59 60 |
# File 'lib/rumonade/option.rb', line 58 def bind(lam = nil, &blk) empty? ? self : (lam || blk).call(value) end |
#empty? ⇒ Boolean
Returns true
if None
, false
if Some
65 66 67 |
# File 'lib/rumonade/option.rb', line 65 def empty? raise(NotImplementedError) end |
#get ⇒ Object
Returns contents if Some, or raises NoSuchElementError if None
70 71 72 |
# File 'lib/rumonade/option.rb', line 70 def get if !empty? then value else raise NoSuchElementError end end |
#get_or_else(val_or_lam = nil, &blk) ⇒ Object
Returns contents if Some, or given value or result of given block or lambda if None
75 76 77 78 |
# File 'lib/rumonade/option.rb', line 75 def get_or_else(val_or_lam = nil, &blk) v_or_f = val_or_lam || blk if !empty? then value else (v_or_f.respond_to?(:call) ? v_or_f.call : v_or_f) end end |
#or_nil ⇒ Object
Returns contents if Some, or nil
if None
81 82 83 |
# File 'lib/rumonade/option.rb', line 81 def or_nil get_or_else(nil) end |