Class: Fixnum

Inherits:
Object show all
Defined in:
lib/rdoba/numeric.rb

Instance Method Summary collapse

Instance Method Details

#_rdoba_to_sObject



26
# File 'lib/rdoba/numeric.rb', line 26

alias :_rdoba_to_s :to_s

#to_s(base = 10, *opts) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rdoba/numeric.rb', line 27

def to_s(base = 10, *opts)
  v = parse_opts(opts)

  return _rdoba_to_s(base) unless v[:padding] or v[:style_formatting]

  raise "Base of number can't be equal or less then zero" if base <= 0
  raise "Padding count numberr can't be equal or less then zero" if v[:padding] <= 0
  value = self
  minus = if value < 0
      value = -value
      true
    end
  res = ''
  while value != 0
    value, rem = value.divmod(base)
    rem += 0x40 - 0x39 if rem >= 10
    res += (0x30 + rem).chr
  end
  res += "0" * (v[:padding].to_i - res.size) if res.size < v[:padding].to_i
  res += 'x0' if v[:style_formatting] and base == 16
  res += '-' if minus
  res.reverse
end