Class: Rumonade::Option Abstract

Inherits:
Object
  • Object
show all
Includes:
Monad
Defined in:
lib/rumonade/option.rb

Overview

This class is abstract.

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

Direct Known Subclasses

NoneClass, Some

Constant Summary

Constants included from Monad

Monad::DEFAULT_METHODS_TO_REPLACE_WITH_MONAD

Class Method Summary collapse

Instance Method Summary collapse

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

.emptyOption

Returns the empty Option

Returns:

  • (Option)

    Returns the empty Option


47
48
49
# File 'lib/rumonade/option.rb', line 47

def empty
  None
end

.unit(value) ⇒ Option

Returns an Option containing the given value

Returns:

  • (Option)

    Returns an Option containing the given value


42
43
44
# File 'lib/rumonade/option.rb', line 42

def unit(value)
  Rumonade.Some(value)
end

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

Returns:

  • (Boolean)

    Returns true if None, false if Some

Raises:

  • (NotImplementedError)

65
66
67
# File 'lib/rumonade/option.rb', line 65

def empty?
  raise(NotImplementedError)
end

#getObject

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_nilObject

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