module MissingMath::Number

Methods that can apply to all number object types

Public Instance Methods

append(n) click to toggle source

Returns the number with a number added to the end of the number @param integer n The number to append Example: 1234.append(5) => 12345

# File lib/missing_math.rb, line 43
def append(n)
  str = "#{self}#{n}"
  number_out(str)
end
contains?(search) click to toggle source

Checks if the number contains a digit sequence or regex @param integer|regexp search Example 1: 12345.contains?(34) => true Example 2: 12345.contains?(/34/) => true

# File lib/missing_math.rb, line 78
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() click to toggle source

Returns the number of digits in the number (if a float, decimal is excluded) Example: 12345.length => 5

# File lib/missing_math.rb, line 10
def length
  str = self.to_s
  if str.index('.')
    str.length - 1
  elsif
    str.length
  end
end
palindrome?() click to toggle source

Returns true|false if the number is a palindrome Example: 123454321.palindrome? => true

# File lib/missing_math.rb, line 111
def palindrome?
  return self == self.reverse
end
pop() click to toggle source

Returns the number with the last digit removed from the end of the number Similar to an Array#pop method Example: 12345.pop => 1234

# File lib/missing_math.rb, line 59
def pop
  str = self.to_s[0..-2]
  number_out(str)
end
prepend(n) click to toggle source

Returns the number with a number added to the beginning of the number @param integer n The number to prepend Example: 2345.prepend(1) => 12345

# File lib/missing_math.rb, line 35
def prepend(n)
  str = "#{n}#{self}"
  number_out(str)
end
reverse() click to toggle source

Returns the number reversed Example: 12345.reverse => 54321

# File lib/missing_math.rb, line 103
def reverse
  a = self.to_a
  str = a.reverse.join('')
  number_out(str)
end
rotate(right=true) click to toggle source

Returns the number with the first digit moved to the end @param boolean right Defaults to rotating right. Set to false to rotate left Example 1: 12345.rotate => 23451 Example 2: 12345.rotate(false) => 51234

# File lib/missing_math.rb, line 90
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() click to toggle source

Returns the number with the first digit removed from the beginning of the number Similar to an Array#shift method Example: 12345.shift => 2345

# File lib/missing_math.rb, line 51
def shift
  str = self.to_s[1..-1]
  number_out(str)
end
substr(search) click to toggle source

Returns a digit subset/substring of the current number with a 0-indexed integer or range @param integer|range search The substring search to return Similar to a String object's substring methods. Example 1: 12345.substr(2) => 3 Example 2: 12345.substr(0..2) => 123

# File lib/missing_math.rb, line 69
def substr(search)
  str = self.to_s[search]
  number_out(str)
end
to_a() click to toggle source

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]

# File lib/missing_math.rb, line 21
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

Private Instance Methods

number_out(str) click to toggle source

Output a stringed version of a number as a float or integer (whichever it deserves)

# File lib/missing_math.rb, line 119
def number_out(str)
  i = str.to_i
  f = str.to_f
  if i == f
    return i
  else
    return f
  end
end