class TracLang::Octal
TRAC octal number. Unlike the TracLang::Decimal
, any string prefix is discarded. Bit size is saved so that operations can retain the size.
Attributes
size[RW]
Number of octal digits in this number.
value[RW]
Numeric value of this number.
Public Class Methods
new(str = '')
click to toggle source
Creates a TRAC octal. Any leading non-numeric characters are ignored.
If there are no octal characters, zero is assumed.
# File lib/trac_lang/octal.rb, line 17 def initialize(str = '') raise ArgumentError unless str.is_a? String b = str.partition(/[0-7]*$/) @value = b[1].to_i(8) @size = b[1].length end
Public Instance Methods
==(o)
click to toggle source
Tests for equality. This is different from numeric equality, because size is also tested.
Calls superclass method
# File lib/trac_lang/octal.rb, line 26 def ==(o) return super unless o.is_a? TracLang::Octal o.value == @value && o.size == @size end
rotate(n)
click to toggle source
Rotates octal bits by the given amount.
# File lib/trac_lang/octal.rb, line 73 def rotate(n) result = Octal.new result.size = size bits = 3 * size bit_mask = 2 ** bits - 1 if n.value < 0 result.value = ((value >> -n.value) | (value << (bits + n.value))) & bit_mask else result.value = ((value << n.value) | (value >> (bits - n.value))) & bit_mask end result end
shift(n)
click to toggle source
Shifts octal bits by the given amount.
# File lib/trac_lang/octal.rb, line 65 def shift(n) result = Octal.new result.size = size result.value = n.value < 0 ? value >> -n.value : value << n.value result end
to_s()
click to toggle source
Converts octal number to string. Returns a string <size> digits long.
# File lib/trac_lang/octal.rb, line 32 def to_s if value < 0 sprintf("%0*.*o", size + 3, size + 3, value).slice(3..-1) else sprintf("%0*.*o", size, size, value) end end
~()
click to toggle source
Bit complement.
# File lib/trac_lang/octal.rb, line 57 def ~ result = Octal.new result.size = size result.value = ~value result end