Class: Integer
- Includes:
- Random::IntegerExtensions
- Defined in:
- lib/core/facets/roman.rb,
lib/core/facets/integer/of.rb,
lib/core/facets/integer/bitmask.rb,
lib/core/facets/integer/ordinal.rb,
lib/core/facets/integer/multiple.rb,
lib/core/facets/integer/factorial.rb,
lib/standard/facets/random.rb
Instance Method Summary collapse
-
#bit(bit) ⇒ Object
Set a bit.
-
#bit?(bit) ⇒ Boolean
Is a bit set?.
-
#bit_clear(bit) ⇒ Object
Clear bit.
-
#bitmask(mask) ⇒ Object
Apply a bitmask.
-
#bitmask?(mask) ⇒ Boolean
Is bitmask set?.
-
#factorial ⇒ Object
(also: #fac)
Calculate the factorial of an integer.
-
#multiple?(number) ⇒ Boolean
Is
self
a multiple of a given number?. -
#of(&block) ⇒ Object
(also: #times_collect, #times_map)
Like #times but returns a collection of the yield results.
- #ordinal ⇒ Object (also: #ordinalize)
-
#roman ⇒ Object
Converts this integer to a roman numeral.
Methods included from Random::IntegerExtensions
Instance Method Details
#bit(bit) ⇒ Object
Set a bit.
0.bit(4) #=> 16
Using a negative figure will clear a bit.
10.bit(-4) #=> 2
This is more easily seen using binary.
0b0100.bit(-3) #=> 0
CREDIT: Thomas Sawyer, George Moschovitis
17 18 19 20 21 22 23 24 25 |
# File 'lib/core/facets/integer/bitmask.rb', line 17 def bit(bit) if bit < 0 mask = (1 << ~bit) self & ~mask else mask = (1 << bit) self | mask end end |
#bit?(bit) ⇒ Boolean
Is a bit set?
8.bit?(3) #=> true
8.bit?(2) #=> false
CREDIT: Thomas Sawyer, George Moschovitis
43 44 45 46 |
# File 'lib/core/facets/integer/bitmask.rb', line 43 def bit?(bit) mask = (1 << bit) (self & mask) != 0 end |
#bit_clear(bit) ⇒ Object
Clear bit.
CREDIT: George Moschovitis
31 32 33 34 |
# File 'lib/core/facets/integer/bitmask.rb', line 31 def bit_clear(bit) mask = (1 << bit) self & ~mask end |
#bitmask(mask) ⇒ Object
Apply a bitmask.
1.bitmask(6) #=> 7
Using a inverted bitmask clears bits.
7.bitmask(~2) #=> 5
5.bitmask(~2) #=> 5
CREDIT: George Moschovitis
59 60 61 62 63 64 65 |
# File 'lib/core/facets/integer/bitmask.rb', line 59 def bitmask(mask) if mask < 0 self & mask else self | mask end end |
#bitmask?(mask) ⇒ Boolean
Is bitmask set?
7.bitmask?(7) #=> true
7.bitmask?(5) #=> true
8.bitmask?(3) #=> false
CREDIT: George Moschovitis
75 76 77 |
# File 'lib/core/facets/integer/bitmask.rb', line 75 def bitmask?(mask) (self & mask) != 0 end |
#factorial ⇒ Object Also known as: fac
Calculate the factorial of an integer.
2.factorial #=> 2
3.factorial #=> 6
4.factorial #=> 24
CREDIT: Malte Milatz
11 12 13 14 15 16 |
# File 'lib/core/facets/integer/factorial.rb', line 11 def factorial return 1 if zero? f = 1 2.upto(self) { |n| f *= n } f end |
#multiple?(number) ⇒ Boolean
Is self
a multiple of a given number?
7.multiple?(2) #=> false
8.multiple?(2) #=> true
CREDIT: Trans
10 11 12 13 14 15 16 |
# File 'lib/core/facets/integer/multiple.rb', line 10 def multiple?(number) if number.zero? zero? ? true : false else self % number == 0 end end |
#of(&block) ⇒ Object Also known as: times_collect, times_map
Like #times but returns a collection of the yield results.
a = 3.of { |i| "#{i+1}" }
a #=> [ "1", "2", "3" ]
9 10 11 |
# File 'lib/core/facets/integer/of.rb', line 9 def of(&block) Array.new(self, &block) end |
#ordinal ⇒ Object Also known as: ordinalize
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/core/facets/integer/ordinal.rb', line 3 def ordinal if [11,12,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 |
#roman ⇒ Object
Converts this integer to a roman numeral.
NOTE: This method is not a common core extension and is not loaded automatically when using require 'facets'
.
31 32 33 34 35 36 37 38 39 |
# File 'lib/core/facets/roman.rb', line 31 def roman int = self #return nil if integer > ROMAN_MAX return "-#{(-int).roman}" if int < 0 return "" if int == 0 ROMAN_VALUES.each do |(i, v)| return(i + (int-v).roman) if v <= int end end |