Sha256: baa033d1ee6a2f56f4a3663b593d2cbf8c1d38cc7cf1118b37d7e21154a42843

Contents?: true

Size: 1012 Bytes

Versions: 1

Compression:

Stored size: 1012 Bytes

Contents

= Radix Integer

Radix provides an Integer class for working with integers in various bases.

  require 'radix'

Radix extend the Integer, String and Array classes with the #b method
which simplifies the creation of Radix::Integer instances. The following
return the equivalent instance of Radix::Integer.

  a = 8.b(2)

  b = "1000".b(2)

  c = [1, 0, 0, 0].b(2)

  a.assert == b
  b.assert == c
  c.assert == a

  a.assert == 8
  b.assert == 8
  c.assert == 8

Radix integers can ve converted to other bases with the #convert method.

  b = "1000".b(2)
  d = b.convert(10)
  d.digits.assert == [8]

Radix::Integer supports all the usual mathematical operators.

  r = "1000".b(2) + "2".b(8)
  r.assert == "1010".b(2)
  r.assert == "12".b(8)
  r.assert == "10".b(10)

A more complex example with a much higher base.

  r = "AZ42".b(62) + "54".b(10)
  r.assert == "2518124".b(10)
  r.assert == 2518124

Work with arrays for bases greater than 62.

  r = [100,10].b(256) + "54".b(10)
  r.assert == "25664".b(10)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
radix-2.0.0 doc/02_integer.rdoc