Class: Types::Array
- Inherits:
-
TypedReference
show all
- Extended by:
- Forwardable
- Defined in:
- lib/solidity/typed/array.rb,
lib/solidity/typed/array_builder.rb
Constant Summary
Constants inherited
from Typed
Typed::ADDRESS_ZERO, Typed::BYTES20_ZERO, Typed::BYTES32_ZERO, Typed::BYTES_ZERO, Typed::INSCRIPTION_ID_ZERO, Typed::STRING_ZERO
Class Method Summary
collapse
Instance Method Summary
collapse
#==, #eql?, #hash
Methods inherited from Typed
#as_json, dump, serialize, #serialize, #type, type
Constructor Details
#initialize(initial_value = []) ⇒ Array
Returns a new instance of Array.
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/solidity/typed/array.rb', line 22
def initialize( initial_value = [] )
raise ArgumentError, "expected literal of type #{type}; got typed #{initial_value.pretty_print_inspect}" if initial_value.is_a?( Typed )
@data = type.check_and_normalize_literal( initial_value ).map do |item|
type.sub_type.new( item )
end
if type.size > 0 if @data.size >= type.size else
self.size = type.size
end
end
end
|
Class Method Details
.build_class(sub_type, size = 0) ⇒ Object
Also known as:
new
note: add size option here (size=0) default is dynamic (not fixed)!!!
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/solidity/typed/array_builder.rb', line 6
def self.build_class( sub_type, size=0 )
sub_type = typeof( sub_type )
type = ArrayType.instance( sub_type, size )
class_name = type.typedclass_name
cache = @@cache ||= {}
klass = cache[ class_name ]
if klass.nil?
klass = Class.new( Array )
klass.define_singleton_method( :type ) do
@type ||= type
end
klass.define_singleton_method( :new ) do |*args|
old_new( *args )
end
cache[ class_name ] = klass
Types.const_set( class_name, klass )
end
klass
end
|
.zero ⇒ Object
todo/check: make “internal” data (array) available? why? why not? attr_reader :data
9
|
# File 'lib/solidity/typed/array.rb', line 9
def self.zero() @zero ||= new; end
|
Instance Method Details
#[](index) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/solidity/typed/array.rb', line 51
def []( index )
index = index.is_a?( Typed ) ? index.as_data : index
raise ArgumentError, "Index out of bounds - #{index} : #{index.class.name} >= #{@data.size}" if index >= @data.size
obj = @data[ index ]
if obj.nil?
obj = type.sub_type.new_zero
@data[ index ] = obj
end
obj
end
|
#[]=(index, new_value) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/solidity/typed/array.rb', line 66
def []=(index, new_value)
index = index.is_a?( Typed ) ? index.as_data : index
raise ArgumentError, "Sparse arrays are not supported; index out of bounds - sorry" if index >= @data.size
obj = if new_value.is_a?( Typed )
new_value
else
type.sub_type.new( new_value )
end
@data[ index ] = obj
end
|
#as_data ⇒ Object
157
158
159
|
# File 'lib/solidity/typed/array.rb', line 157
def as_data
@data.map {|item| item.as_data }
end
|
#clear ⇒ Object
148
149
150
151
152
153
154
|
# File 'lib/solidity/typed/array.rb', line 148
def clear
@data.clear
self.size = type.size if type.size > 0 self end
|
#delete(index) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/solidity/typed/array.rb', line 109
def delete( index )
index = index.is_a?( Typed ) ? index.as_data : index
raise ArgumentError, "Index out of bounds - #{index} : #{index.class.name} >= #{@data.size}" if index >= @data.size
@data[ index ] = type.sub_type.new_zero
self
end
|
#pop ⇒ Object
83
84
85
86
87
88
89
90
|
# File 'lib/solidity/typed/array.rb', line 83
def pop
@data.pop
end
|
#pretty_print(printer) ⇒ Object
161
162
163
|
# File 'lib/solidity/typed/array.rb', line 161
def pretty_print( printer )
printer.text( "<ref #{type}:#{@data.pretty_print_inspect}>" );
end
|
#push(new_value) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/solidity/typed/array.rb', line 92
def push( new_value )
obj = if new_value.is_a?( Typed )
new_value
else
type.sub_type.new( new_value )
end
@data.push( obj )
@data.size
end
|
#size=(new_size) ⇒ Object
Also known as:
length=
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/solidity/typed/array.rb', line 124
def size=( new_size )
diff = new_size - @data.size
if diff == 0
elsif diff > 0
if type.sub_type.respond_to?( :new_zero )
diff.times { @data << type.sub_type.new_zero }
else
raise "[Array]#length= cannot create new_zero for type #{type.sub_type} - sorry"
end
else end
self end
|
#zero? ⇒ Boolean
10
11
12
13
14
15
16
17
18
|
# File 'lib/solidity/typed/array.rb', line 10
def zero?()
if type.size == 0 @data.empty?
else self == self.class.zero
end
end
|