Module: AverageHelper
- Defined in:
- lib/average/average_helper.rb
Instance Method Summary collapse
- #array_contain_digits?(array) ⇒ Boolean
- #clean_array(array) ⇒ Object
- #looks_like_a_digit?(digit) ⇒ Boolean
- #valid_array?(array) ⇒ Boolean
- #value_can_be_handled?(element) ⇒ Boolean
Instance Method Details
#array_contain_digits?(array) ⇒ Boolean
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/average/average_helper.rb', line 10 def array_contain_digits?(array) # Go through all elements result = true array.each do |array_element| if !value_can_be_handled?(array_element) result = false end end result end |
#clean_array(array) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/average/average_helper.rb', line 37 def clean_array(array) array.each_with_index do |val, index| if val.is_a?(String) val.include?('.') ? (array[index] = val.to_f) : (array[index] = val.to_i) end end array end |
#looks_like_a_digit?(digit) ⇒ Boolean
27 28 29 30 31 32 33 34 35 |
# File 'lib/average/average_helper.rb', line 27 def looks_like_a_digit?(digit) if digit.is_a?(String) && digit !~ /^\s*[+-]?((\d+_?)*\d+(\.(\d+_?)*\d+)?|\.(\d+_?)*\d+)(\s*|([eE][+-]?(\d+_?)*\d+)\s*)$/ # is _not_ a string that could be turned to a digit false else # is a string that could be a digit true end end |
#valid_array?(array) ⇒ Boolean
2 3 4 5 6 7 8 |
# File 'lib/average/average_helper.rb', line 2 def valid_array?(array) # Valid if: # - the class of the parameter 'array' respond to Array # - the parameter 'Array' is not null or empty. # - The array given contains Integers, Floats, and Strings that could be turned to a valid digit. ( array.is_a?(Array) && !array.nil? && !array.empty? && array_contain_digits?(array) ) end |
#value_can_be_handled?(element) ⇒ Boolean
21 22 23 24 |
# File 'lib/average/average_helper.rb', line 21 def value_can_be_handled?(element) # Check if the elements of the array 'look like numbers'. element.is_a?(Integer) || element.is_a?(Float) || looks_like_a_digit?(element) end |