### Home Classes Methods

### In Files

- lib/facets/roman.rb
- lib/facets/integer/odd.rb
- lib/facets/integer/of.rb
- lib/facets/integer/ordinal.rb
- lib/facets/integer/bitmask.rb
- lib/facets/integer/factorial.rb
- lib/facets/integer/multiple.rb

### Parent

Object

### Methods

- #bit
- #bit?
- #bit_clear
- #bitmask
- #bitmask?
- #even?
- #fac
- #factorial
- #multiple?
- #odd?
- #of
- #ordinal
- #ordinalize
- #roman
- #times_collect
- #times_map

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Lazy
- Lazy::Future
- Lazy::Promise
- Lazy::Promise::DIVERGES
- Lazy::PromiseSafe
- Enumerable
- Enumerable::Recursor
- Method
- Method::Composition
- Module
- Module::InstanceFunction
- Array
- Binding
- Class
- Comparable
- Denumerable
- Denumerator
- Dir
- Enumerator
- Exception
- Facets
- FalseClass
- File
- FileTest
- Fixnum
- Float
- Functor
- Hash
- Indexable
- Instance
- Integer
- Kernel
- MatchData
- NA
- NilClass
- Numeric
- Object
- ObjectSpace
- Proc
- Process
- Range
- Regexp
- String
- Struct
- Symbol
- Time
- TrueClass
- UnboundMethod

No matching classes.

# Integer

### Constants

ROMAN_MAX
ROMAN_VALUES

### Public Instance Methods

bit(bit) click to toggle source

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

    # File lib/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) click to toggle source

Is a bit set?

    8.bit?(3)  #=> true
    8.bit?(2)  #=> false

CREDIT: Thomas Sawyer, George Moschovitis

    # File lib/facets/integer/bitmask.rb, line 43
    def bit?(bit)
      mask = (1 << bit)
      (self & mask) != 0
    end

bit_clear(bit) click to toggle source

Clear bit.

CREDIT: George Moschovitis

    # File lib/facets/integer/bitmask.rb, line 31
    def bit_clear(bit)
      mask = (1 << bit)
      self & ~mask
    end

bitmask(mask) click to toggle source

Apply a bitmask.

    1.bitmask(6) #=> 7

Using a inverted bitmask clears bits.

    7.bitmask(~2) #=> 5
    5.bitmask(~2) #=> 5

CREDIT: George Moschovitis

    # File lib/facets/integer/bitmask.rb, line 59
    def bitmask(mask)
      if mask < 0
        self & mask
      else
        self | mask
      end
    end

bitmask?(mask) click to toggle source

Is bitmask set?

    7.bitmask?(7) #=> true
    7.bitmask?(5) #=> true
    8.bitmask?(3) #=> false

CREDIT: George Moschovitis

    # File lib/facets/integer/bitmask.rb, line 75
    def bitmask?(mask)
      (self & mask) != 0
    end

even?() click to toggle source

Returns true if this integer is even, false otherwise.

    2.even?  #=> true
    3.even?  #=> false

CREDIT: Daniel Schierbeck

    # File lib/facets/integer/odd.rb, line 30
    def even?
      self & 1 == 0  #self % 2 == 0
    end

fac() click to toggle source
Alias for: factorial
factorial() click to toggle source

Calculate the factorial of an integer.

    2.factorial  #=> 2
    3.factorial  #=> 6
    4.factorial  #=> 24

CREDIT: Malte Milatz

    # File lib/facets/integer/factorial.rb, line 11
    def factorial
      return 1 if zero?
      f = 1
      2.upto(self) { |n| f *= n }
      f
    end

Also aliased as: fac
multiple?(number) click to toggle source

Is `self` a multiple of a given number?

    7.multiple?(2)  #=> false
    8.multiple?(2)  #=> true

CREDIT: Trans

    # File lib/facets/integer/multiple.rb, line 10
    def multiple?(number)
      if number.zero?
        zero? ? true : false
      else
        self % number == 0
      end
    end

odd?() click to toggle source

Returns true if this integer is odd, false otherwise.

    2.odd?            #=> false
    3.odd?            #=> true

    -99.odd?          # -> true
    -98.odd?          # -> false

CREDIT: Daniel Schierbeck

    # File lib/facets/integer/odd.rb, line 15
    def odd?
      self & 1 == 1  #self % 2 == 1
    end

of(&block) click to toggle source

Like times but returns a collection of the yield results.

    a = 3.of { |i| "#{i+1}" }
    a #=> [ "1", "2", "3" ]

    # File lib/facets/integer/of.rb, line 9
    def of(&block)
      Array.new(self, &block)
    end

Also aliased as: times_collect, times_map
ordinal() click to toggle source

    # File lib/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

Also aliased as: ordinalize
ordinalize() click to toggle source

Rails term.

Alias for: ordinal
roman() click to toggle source

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'`.

@uncommon

    require 'facets/integer/roman'

    # File lib/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

times_collect(&block) click to toggle source

Time warn aliases for of.

Alias for: of
times_map(&block) click to toggle source
Alias for: of

[Validate]

Generated with the Darkfish Rdoc Generator 2.