Class: CraftBook::NBT::EnumerableTag Abstract
- Includes:
- Enumerable
- Defined in:
- lib/craftbook/nbt/enumerable_tag.rb
Overview
Abstract base class for tags that can be enumerated.
Direct Known Subclasses
Constant Summary
Constants inherited from Tag
Tag::TYPE_BYTE, Tag::TYPE_BYTE_ARRAY, Tag::TYPE_COMPOUND, Tag::TYPE_DOUBLE, Tag::TYPE_END, Tag::TYPE_FLOAT, Tag::TYPE_INT, Tag::TYPE_INT_ARRAY, Tag::TYPE_LIST, Tag::TYPE_LONG, Tag::TYPE_LONG_ARRAY, Tag::TYPE_SHORT, Tag::TYPE_STRING
Instance Attribute Summary
Attributes inherited from Tag
Instance Method Summary collapse
-
#[](index) ⇒ Object
Retrieves the child at the given
index
, ornil
if index is out of bounds. -
#[]=(index, value) ⇒ Object
Sets the child element at the given
index
. - #each ⇒ Object
-
#initialize(type, name, *values) ⇒ EnumerableTag
constructor
Creates a new instance of the EnumerableTag class.
-
#push(child) ⇒ Object
(also: #<<, #add)
Appends a value as a child of this instance.
-
#size ⇒ Integer
(also: #length)
The number of child elements.
-
#to_h ⇒ Hash{Symbol => Object}
The hash-representation of this object.
Methods inherited from Tag
parse, #pretty, #pretty_print, #stringify, #to_json
Constructor Details
#initialize(type, name, *values) ⇒ EnumerableTag
Creates a new instance of the CraftBook::NBT::EnumerableTag class.
18 19 20 21 22 |
# File 'lib/craftbook/nbt/enumerable_tag.rb', line 18 def initialize(type, name, *values) super(type, name) @values = Array.new values.each { |value| push(value) } end |
Instance Method Details
#[](index) ⇒ Object
Retrieves the child at the given index
, or nil
if index is out of bounds.
67 68 69 |
# File 'lib/craftbook/nbt/enumerable_tag.rb', line 67 def [](index) @values[index] end |
#[]=(index, value) ⇒ Object
Unlike normal Array object, when index is beyond the bounds of the collection, it will not insert nil
elements to fill the space, the new item is simply appended to the end of the collection.
Sets the child element at the given index
.
82 83 84 85 |
# File 'lib/craftbook/nbt/enumerable_tag.rb', line 82 def []=(index, value) validate(value) @values[index] = value end |
#each {|child| ... } ⇒ self #each ⇒ Enumerable
44 45 46 47 48 |
# File 'lib/craftbook/nbt/enumerable_tag.rb', line 44 def each return enum_for(__method__) unless block_given? @values.compact.each { |child| yield child } self end |
#push(child) ⇒ Object Also known as: <<, add
Appends a value as a child of this instance.
31 32 33 |
# File 'lib/craftbook/nbt/enumerable_tag.rb', line 31 def push(child) @values.push(validate(child)) end |
#size ⇒ Integer Also known as: length
Returns the number of child elements.
58 59 60 |
# File 'lib/craftbook/nbt/enumerable_tag.rb', line 58 def size @values.compact.size end |
#to_h ⇒ Hash{Symbol => Object}
Returns the hash-representation of this object.
52 53 54 |
# File 'lib/craftbook/nbt/enumerable_tag.rb', line 52 def to_h { name: @name, type: @type, values: @values } end |