Class: RFunc::AbstractOption

Inherits:
Object
  • Object
show all
Defined in:
lib/rfunc/option.rb

Direct Known Subclasses

None, Some

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ AbstractOption

Initializes the class

Parameters:

  • value (Any)

    the initial value of the Option



11
12
13
# File 'lib/rfunc/option.rb', line 11

def initialize(value)
  @value = value
end

Instance Method Details

#==(object) ⇒ Boolean

Comparator that allows one Option to be compared against another by value

Returns:

  • (Boolean)

    true if the Option values are identical and false if not



21
22
23
# File 'lib/rfunc/option.rb', line 21

def ==(object)
  object.class == self.class && object.get == @value
end

#filter(&block) ⇒ RFunc::Option Also known as: find

Filters the Some by a given function

Parameters:

  • block (Function)

    the function which will operate on the option’s value if present (should return Bool) and be used to determine if a Some of None will be returned

Returns:



38
39
40
# File 'lib/rfunc/option.rb', line 38

def filter(&block)
  map {|el| yield(el)}.get_or_else { false } ? self : None.new
end

#filter_not(&block) ⇒ RFunc::Option

Filters the Some by the inverse of a given function

Parameters:

  • block (Function)

    the function which will operate on the option’s value if present (should return Bool) and be used to determine if a Some of None will be returned

Returns:



50
51
52
# File 'lib/rfunc/option.rb', line 50

def filter_not(&block)
  map {|el| !yield(el) }.get_or_else { false } ? self : None.new
end

#getAny

Extracts the value of the option

Returns:

  • (Any)

    the value of the option



28
# File 'lib/rfunc/option.rb', line 28

def get; @value end

#is_option?(el) ⇒ Boolean

Tests whether or not an object is an Option

Parameters:

  • el (Any)

    the object to test

Returns:

  • (Boolean)

    true if the object is an Option or false if not



62
63
64
# File 'lib/rfunc/option.rb', line 62

def is_option?(el)
  el.is_a?(AbstractOption)
end