Methods
Public Instance methods
Is an integer even?
2.even? #=> true 3.even? #=> false
[ show source ]
# File lib/facets/core/integer/multiple.rb, line 18 def even? self % 2 == 0 end
Alias for factorial
Alias for factorial
Calculate the factorial of an integer.
2.factorial #=> 2 3.factorial #=> 6 3.factorial #=> 24
[ show source ]
# File lib/facets/core/integer/factorial.rb, line 14 def factorial return 1 if zero? f = 1 2.upto(self) { |n| f *= n } f end
Is is a multiple of a given number?
7.multiple?(2) #=> false 8.multiple?(2) #=> true
[ show source ]
# File lib/facets/core/integer/multiple.rb, line 27 def multiple?(number) self % number == 0 end
Is an integer odd?
2.odd? #=> false 3.odd? #=> true
[ show source ]
# File lib/facets/core/integer/multiple.rb, line 9 def odd? self % 2 == 1 end
[ show source ]
# File lib/facets/core/integer/ordinal.rb, line 4 def ordinal if (11..13).include?(self % 100) "#{self}th" else case self % 10 when 1: "#{self}st" when 2: "#{self}nd" when 3: "#{self}rd" else "#{self}th" end end end
[ show source ]
# File lib/facets/core/float/round_at.rb, line 29 def round_at(*args) self.to_f.round_at(*args) end
[ show source ]
# File lib/facets/core/float/round_to.rb, line 30 def round_to(*args) self.to_f.round_to(*args) end
Like times but returns a collection of the yield results.
a = 3.times_collect { |i| "#{i+1}" } a => [ "1", "2", "3" ]
This method is also aliased as
times_map
[ show source ]
# File lib/facets/core/integer/times_collect.rb, line 9 def times_collect(&yld) a = []; self.times{ |i| a << yld.call(i) } a end
Alias for times_collect