Module: BaseConvert::Methods

Extended by:
Methods
Included in:
FromTo, Methods, Number
Defined in:
lib/base_convert/methods.rb

Instance Method Summary collapse

Instance Method Details

#chars_ordered?(digits = @digits) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/base_convert/methods.rb', line 20

def chars_ordered?(digits=@digits)
  digits.chars.each_cons(2).all?{_1<_2}
end

#toi(string = to_s, base = @base, digits = @digits) ⇒ Object



4
5
6
7
# File 'lib/base_convert/methods.rb', line 4

def toi(string=to_s, base=@base, digits=@digits)
  return nil if string.empty?
  string.chars.inject(0){_1*base + digits.index(_2)}
end

#tos(integer = to_i, base = @base, digits = @digits) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/base_convert/methods.rb', line 9

def tos(integer=to_i, base=@base, digits=@digits)
  return '' if integer.nil?
  return digits[0] if integer == 0
  string = ''
  while integer > 0
    integer, index = integer.divmod(base)
    string = string.prepend digits[index]
  end
  string
end