Class: Integer

Inherits:
Object show all
Defined in:
lib/lite/ruby/integer.rb

Instance Method Summary collapse

Instance Method Details

#factorialObject



10
11
12
13
14
# File 'lib/lite/ruby/integer.rb', line 10

def factorial
  return 1 if zero?

  2.upto(self).inject(1) { |acc, i| acc * i }
end

#factorsObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lite/ruby/integer.rb', line 16

def factors
  limit = Math.sqrt(self).floor

  (1..limit).each_with_object([]) do |i, array|
    next unless (self % i).zero?

    sq = (self / i)
    array.push(i)
    array.push(sq) if sq != i
  end
end

#of(&block) ⇒ Object



28
29
30
# File 'lib/lite/ruby/integer.rb', line 28

def of(&block)
  Array.new(self, &block)
end

#roman_numeralObject



32
33
34
35
36
37
# File 'lib/lite/ruby/integer.rb', line 32

def roman_numeral
  return '' if zero?
  return "-#{(-self).roman_numeral}" if negative?

  ROMAN_NUMERALS.each { |key, val| break "#{key}#{(self - val).roman_numeral}" if val <= self }
end

#to_timeObject Also known as: to_t



39
40
41
# File 'lib/lite/ruby/integer.rb', line 39

def to_time
  Time.at(self)
end