Class: Integer
Instance Method Summary collapse
- #factorial ⇒ Object
- #factors ⇒ Object
- #of(&block) ⇒ Object
- #roman_numeral ⇒ Object
- #to_time ⇒ Object (also: #to_t)
Instance Method Details
#factorial ⇒ Object
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 |
#factors ⇒ Object
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_numeral ⇒ Object
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 |