Module: ToBinary

Included in:
Integer
Defined in:
lib/to_binary.rb

Instance Method Summary collapse

Instance Method Details

#to_binary(length = 0) ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'lib/to_binary.rb', line 2

def to_binary length = 0
  n = self
  b = [self == 0 ? 0 : nil]
  while n > 0
    b.unshift n % 2
    n /= 2
  end
  (length-1).times { b.unshift 0 }
  b.join
end