Module: Rubel::Functions::Defaults

Included in:
Base
Defined in:
lib/rubel/functions/defaults.rb

Overview

Default/standard functions like SUM,AVG,COUNT,etc that operate on numbers and are application independent.

Instance Method Summary collapse

Instance Method Details

#AVG(*values) ⇒ Numeric

Returns the average of all number (ignores nil values).

Examples:

AVG(1,2)          # => 1.5
AVG(1,2,3)        # => 2
AVG(1,nil,nil,2)  # => 1.5

Parameters:

  • *values (Numeric, Array)

    one or multiple values or arrays

Returns:

  • (Numeric)

    The average of all values



58
59
60
61
62
# File 'lib/rubel/functions/defaults.rb', line 58

def AVG(*values)
  values.flatten!
  values.compact!
  SUM(values) / COUNT(values)
end

#COUNT(*values) ⇒ Numeric

Returns how many values. Removes nil values, but does not remove duplicates.

Examples:

Basic useage

COUNT(1)              # => 1
COUNT(1,2)            # => 1

with converters

COUNT(L(foo,bar))     # => 2

multiple LOOKUPs (does not remove duplicates)

COUNT(L(foo,bar), L(foo))     # => 3
# However: (LOOKUP removes duplicates)
COUNT(L(foo,bar,foo), L(f))   # => 2

nil values are removed (do not count)

COUNT(1,nil,2) # => 2

Parameters:

  • *values (Numeric, Array)

    one or multiple values or arrays

Returns:

  • (Numeric)

    The element count.



41
42
43
44
45
46
# File 'lib/rubel/functions/defaults.rb', line 41

def COUNT(*values)
  values.flatten!
  values.compact!

  values.length
end

#DIVIDE(*values) ⇒ Numeric

Divides the first with the second.

Examples:

DIVIDE(1,2)      # => 0.5
DIVIDE(1,2,3,4)  # => 0.5 # only takes the first two numbers
DIVIDE([1,2])    # => 0.5
DIVIDE([1],[2])  # => 0.5
DIVIDE(1,2)      # => 0.5

Watch out doing normal arithmetics (outside DIVIDE)

DIVIDE(2,3)      # => 0.66
# (divideing integers gets you elimentary school output. 2 / 3 = 0 with remainder 2)
2 / 3            # => 0
2 % 3            # => 2 # % = modulo (what is the remainder)
2.0 / 3          # => 0.66 If one number is a float it works as expected
2 / 3.0          # => 0.66 If one number is a float it works as expected

Exceptions

DIVIDE(nil, 1)   # => 0.0
DIVIDE(0.0, 1)   # => 0.0 and not NaN
DIVIDE(0,   1)   # => 0.0 and not NaN
DIVIDE(1.0,0.0)  # => Infinity

Parameters:

  • *values (Numeric, Array)

    one or multiple values or arrays. But only the first two are taken.

Returns:

  • (Numeric)

    The average of all values



125
126
127
128
129
130
131
132
133
# File 'lib/rubel/functions/defaults.rb', line 125

def DIVIDE(*values)
  a,b = values.tap(&:flatten!)

  if a.nil? || a.to_f == 0.0
    0.0
  else
    a.to_f / b
  end
end

#MAP(elements, attr_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rubel/functions/defaults.rb', line 6

def MAP(elements, attr_name)
  elements = [elements] unless elements.is_a?(::Array) 
  
  elements.tap(&:flatten!).map! do |a| 
    if attr_name.respond_to?(:call)
       a.instance_exec(&attr_name)
    else
      # to_s imported, for when MAP(..., demand) demand comes through method_missing (as a symbol)
      a.instance_eval(attr_name.to_s)
    end
  end
  elements.length <= 1 ? (elements.first || 0.0) : elements
end

#PRODUCT(*values) ⇒ Numeric

Multiplies all numbers (ignores nil values).

Examples:

PRODUCT(1,2)     # => 2 (1*2)
PRODUCT(1,2,3)   # => 6 (1*2*3)
PRODUCT(1)       # => 1
PRODUCT(1,nil)   # => 1

Parameters:

  • *values (Numeric, Array)

    one or multiple values or arrays

Returns:

  • (Numeric)

    The average of all values



92
93
94
95
96
# File 'lib/rubel/functions/defaults.rb', line 92

def PRODUCT(*values)    
  values.flatten!
  values.compact!
  values.inject(1) {|total,value| total = total * value}
end

#SUM(*values) ⇒ Numeric

Returns the sum of all numbers (ignores nil values).

Examples:

SUM(1,2)          # => 3
SUM(1,2,3)        # => 6
SUM(1)            # => 1
SUM(1,nil)        # => 1

Parameters:

  • *values (Numeric, Array)

    one or multiple values or arrays

Returns:

  • (Numeric)

    The average of all values



75
76
77
78
79
# File 'lib/rubel/functions/defaults.rb', line 75

def SUM(*values)
  values.flatten!
  values.compact!
  values.inject(0) {|h,v| h + v }
end