Class: NBTFile::Types::List

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Private::Base
Defined in:
lib/nbtfile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, items = []) ⇒ List

Returns a new instance of List.



917
918
919
920
921
922
923
# File 'lib/nbtfile.rb', line 917

def initialize(type, items=[])
  @type = type
  @items = []
  for item in items
    self << item
  end
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



915
916
917
# File 'lib/nbtfile.rb', line 915

def type
  @type
end

Instance Method Details

#<<(item) ⇒ Object



925
926
927
928
929
930
931
# File 'lib/nbtfile.rb', line 925

def <<(item)
  unless item.instance_of? @type
    raise TypeError, "Items should be instances of #{@type}"
  end
  @items << item
  self
end

#==(other) ⇒ Object



951
952
953
# File 'lib/nbtfile.rb', line 951

def ==(other)
  self.class == other.class && @items == other.to_a
end

#eachObject



933
934
935
936
937
938
939
940
# File 'lib/nbtfile.rb', line 933

def each
  if block_given?
    @items.each { |item| yield item }
    self
  else
    @items.each
  end
end

#lengthObject Also known as: size



946
947
948
# File 'lib/nbtfile.rb', line 946

def length
  @items.length
end

#to_aObject



942
943
944
# File 'lib/nbtfile.rb', line 942

def to_a
  @items.dup
end