Module: MissingMath::Number
Overview
Methods that can apply to all number object types
Instance Method Summary collapse
-
#append(n) ⇒ Object
Returns the number with a number added to the end of the number Example: 1234.append(5) => 12345.
-
#contains?(search) ⇒ Boolean
Checks if the number contains a digit sequence or regex Example 1: 12345.contains?(34) => true Example 2: 12345.contains?(/34/) => true.
-
#length ⇒ Object
Returns the number of digits in the number (if a float, decimal is excluded) Example: 12345.length => 5.
-
#pop ⇒ Object
Returns the number with the last digit removed from the end of the number Similar to an Array#pop method Example: 12345.pop => 1234.
-
#prepend(n) ⇒ Object
Returns the number with a number added to the beginning of the number Example: 2345.prepend(1) => 12345.
-
#rotate(right = true) ⇒ Object
Returns the number with the first digit moved to the end Example 1: 12345.rotate => 23451 Example 2: 12345.rotate(false) => 51234.
-
#shift ⇒ Object
Returns the number with the first digit removed from the beginning of the number Similar to an Array#shift method Example: 12345.shift => 2345.
-
#substr(search) ⇒ Object
Returns a digit subset/substring of the current number with a 0-indexed integer or range Similar to a String object’s substring methods.
-
#to_a ⇒ Object
Converts an number to an array of numbers.
Instance Method Details
#append(n) ⇒ Object
Returns the number with a number added to the end of the number Example: 1234.append(5) => 12345
27 28 29 30 |
# File 'lib/missing_math.rb', line 27 def append(n) str = "#{self}#{n}" number_out(str) end |
#contains?(search) ⇒ Boolean
Checks if the number contains a digit sequence or regex Example 1: 12345.contains?(34) => true Example 2: 12345.contains?(/34/) => true
77 78 79 80 81 82 83 |
# File 'lib/missing_math.rb', line 77 def contains?(search) if search.is_a? Regexp return self.to_s.scan(search).length > 0 ? true : false else return self.to_s.index(search.to_s) ? true : false end end |
#length ⇒ Object
Returns the number of digits in the number (if a float, decimal is excluded) Example: 12345.length => 5
7 8 9 10 11 12 13 14 |
# File 'lib/missing_math.rb', line 7 def length str = self.to_s if str.index('.') str.length - 1 elsif str.length end end |
#pop ⇒ Object
Returns the number with the last digit removed from the end of the number Similar to an Array#pop method Example: 12345.pop => 1234
43 44 45 46 |
# File 'lib/missing_math.rb', line 43 def pop str = self.to_s[0..-2] number_out(str) end |
#prepend(n) ⇒ Object
Returns the number with a number added to the beginning of the number Example: 2345.prepend(1) => 12345
19 20 21 22 |
# File 'lib/missing_math.rb', line 19 def prepend(n) str = "#{n}#{self}" number_out(str) end |
#rotate(right = true) ⇒ Object
Returns the number with the first digit moved to the end Example 1: 12345.rotate => 23451 Example 2: 12345.rotate(false) => 51234
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/missing_math.rb', line 62 def rotate(right=true) a = self.to_a if right a.push(a.shift) elsif !right a.unshift(a.pop) end str = a.join('') number_out(str) end |
#shift ⇒ Object
Returns the number with the first digit removed from the beginning of the number Similar to an Array#shift method Example: 12345.shift => 2345
35 36 37 38 |
# File 'lib/missing_math.rb', line 35 def shift str = self.to_s[1..-1] number_out(str) end |
#substr(search) ⇒ Object
Returns a digit subset/substring of the current number with a 0-indexed integer or range Similar to a String object’s substring methods. Example 1: 12345.substr(2) => 3 Example 2: 12345.substr(0..2) => 123
53 54 55 56 |
# File 'lib/missing_math.rb', line 53 def substr(search) str = self.to_s[search] number_out(str) end |
#to_a ⇒ Object
Converts an number to an array of numbers. If a decimal number, the decimal will be a position in the array Example: 12345.to_a => [1, 2, 3, 4, 5]
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/missing_math.rb', line 87 def to_a self.to_s.split('').map do |n| matches = n.scan(/[0-9]/).length if matches > 0 n.to_i else n end end end |