Class: NBTFile::Types::Private::BaseInteger

Inherits:
BaseScalar
  • Object
show all
Defined in:
lib/nbtfile.rb

Instance Attribute Summary

Attributes inherited from BaseScalar

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseScalar

#<=>

Constructor Details

#initialize(value) ⇒ BaseInteger

Returns a new instance of BaseInteger.



799
800
801
802
803
804
805
806
807
808
# File 'lib/nbtfile.rb', line 799

def initialize(value)
  unless self.class::RANGE.include? value
    raise RangeError, "Value out of range"
  end
  int_value = value.to_int
  if int_value != value
    raise TypeError, "Not an integer"
  end
  @value = value
end

Class Method Details

.make_subclass(n_bits) ⇒ Object



792
793
794
795
796
797
# File 'lib/nbtfile.rb', line 792

def self.make_subclass(n_bits)
  subclass = Class.new(self)
  limit = 1 << (n_bits - 1)
  subclass.const_set(:RANGE, -limit..(limit-1))
  subclass
end

Instance Method Details

#==(other) ⇒ Object



810
811
812
813
814
815
816
# File 'lib/nbtfile.rb', line 810

def ==(other)
  if other.respond_to? :to_int
    self.to_int == other.to_int
  else
    false
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


818
819
820
# File 'lib/nbtfile.rb', line 818

def eql?(other)
  other.class == self.class and other.value == @value
end

#hashObject



822
823
824
# File 'lib/nbtfile.rb', line 822

def hash
  [self.class, @value].hash
end