Class: Integer

Class | Integer
In: | lib/core/facets/bitmask.rb lib/core/facets/integer/factorial.rb lib/core/facets/integer/length.rb lib/core/facets/integer/multiple.rb lib/core/facets/integer/odd.rb lib/core/facets/integer/of.rb lib/core/facets/integer/ordinal.rb lib/core/facets/numeric/round.rb lib/more/facets/succ.rb
Parent: | Object

Calculate the number of digits in an integer.

1.length #=> 1 10.length #=> 2 100.length #=> 3

CREDIT: Victor H. Goff III

### Methods

bit bit! bit? bitmask bitmask! bitmask? clear_bit even? fac factorial length multiple? odd? of ordinal ordinalize pred round_at round_to succ times_collect times_map

### Public Instance methods

bit(bit)

Set a bit.

      0.bit!(4)  #=> 8

Using an inverted bit will clear a bit.

      10.bit!(~3)      #=> 2
      0xb0100.bit(~3)  #=> 0

CREDIT: Thomas Sawyer, George Moschovitis

[Source]

    # File lib/core/facets/bitmask.rb, line 14
      def bit(bit)
        if bit < 0
          mask = (1 << ~bit)
          self & ~mask
        else
          mask = (1 << bit)
          self | mask
        end
      end

bit!(bit)

Alias for bit

bit?(bit)

Is a bit set?

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

CREDIT: Thomas Sawyer, George Moschovitis

[Source]

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

bitmask(mask)

Apply a bitmask.

      1.bitmask(6) #=> 7

Using a inverted bitmask clears bits.

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

CREDIT: George Moschovitis

[Source]

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

bitmask!(mask)

Alias for bitmask

bitmask?(mask)

Is bitmask set?

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

CREDIT: George Moschovitis

[Source]

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

clear_bit(bit)

Clear bit.

CREDIT: George Moschovitis

[Source]

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

even?()

Returns true if this integer is even, false otherwise.

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

CREDIT: Daniel Schierbeck

[Source]

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

fac()

Alias for factorial

factorial()

Calculate the factorial of an integer.

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

CREDIT: Malte Milatz

[Source]

    # 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

length()

Returns the length of an integer as an integer.

[Source]

    # File lib/core/facets/integer/length.rb, line 12
      def length
        to_s.length
      end

multiple?(number)

Is is a multiple of a given number?

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

CREDIT: Trans

[Source]

    # File lib/core/facets/integer/multiple.rb, line 10
      def multiple?(number)
        self % number == 0
      end

odd?()

Returns true if this integer is odd, false otherwise.

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

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

CREDIT: Daniel Schierbeck

[Source]

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

of(&block)

Like times but returns a collection of the yield results.

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

[Source]

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

ordinal()

[Source]

    # 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

ordinalize()

Alias for ordinal

pred(n=1)

Provides pred as the opposite of succ.

      3.pred(2)  #=> 1

CREDIT: Trans

[Source]

    # File lib/more/facets/succ.rb, line 19
      def pred(n=1)
        self - n
      end

round_at(*args)

See Float#round_at.

[Source]

    # File lib/core/facets/numeric/round.rb, line 27
      def round_at(*args)
        to_f.round_at(*args)
      end

round_to(*args)

See Float#round_to.

[Source]

    # File lib/core/facets/numeric/round.rb, line 33
      def round_to(*args)
        to_f.round_to(*args)
      end

succ(n=1)

Allows succ to take n increments.

      3.succ(2)  #=> 5

CREDIT: Trans

[Source]

    # File lib/more/facets/succ.rb, line 9
      def succ(n=1)
        self + n
      end

times_collect(&block)

Alias for of

times_map(&block)

Alias for of

[Validate]