Class: TypedArray

Inherits:
Array
  • Object
show all
Defined in:
lib/typed_array.rb,
lib/typed_array/version.rb

Constant Summary collapse

VERSION =
"1.0.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item_class, *args) ⇒ TypedArray

Returns a new instance of TypedArray.



13
14
15
16
17
# File 'lib/typed_array.rb', line 13

def initialize(item_class, *args)
  @item_class = item_class
  validate_assigned_items(*args) if args.size > 0
  super(*args)
end

Instance Attribute Details

#item_classObject (readonly)

Returns the value of attribute item_class.



4
5
6
# File 'lib/typed_array.rb', line 4

def item_class
  @item_class
end

Class Method Details

.[](*args) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
# File 'lib/typed_array.rb', line 6

def self.[](*args)
  klasses = args.compact.map(&:class).uniq
  raise ArgumentError, '[] constructor requires at least one non-nil element' if klasses.empty?
  raise ArgumentError, 'all arguments should be of the same type' if klasses.size > 1
  self.new(klasses.first).push(*args)
end

Instance Method Details

#&(items) ⇒ Object



165
166
167
# File 'lib/typed_array.rb', line 165

def &(items)
  TypedArray.new(item_class).concat(super)
end

#*(*args) ⇒ Object



157
158
159
# File 'lib/typed_array.rb', line 157

def *(*args)
  copy_item_class(super)
end

#+(items) ⇒ Object



152
153
154
155
# File 'lib/typed_array.rb', line 152

def +(items)
  validate_assigned_items(items)
  TypedArray.new(item_class).concat(super)
end

#-(items) ⇒ Object



161
162
163
# File 'lib/typed_array.rb', line 161

def -(items)
  TypedArray.new(item_class).concat(super)
end

#<<(item) ⇒ Object



50
51
52
53
# File 'lib/typed_array.rb', line 50

def <<(item)
  validate_assigned_items([item])
  super
end

#==(item) ⇒ Object



19
20
21
# File 'lib/typed_array.rb', line 19

def ==(item)
  (!item.is_a?(TypedArray) || item_class == item.item_class) && super
end

#[](*args) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/typed_array.rb', line 27

def [](*args)
  if args.size == 2 || (args.size == 1 && args.first.is_a?(Range))
    self.class.new(item_class).push(*super)
  else
    super
  end
end

#[]=(*args) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/typed_array.rb', line 35

def []=(*args)
  if (args.size == 3 || (args.size == 2 && args.first.is_a?(Range))) && args.last.is_a?(Array)
    validate_assigned_items(args.last)
  else
    validate_assigned_items([args.last])
  end

  super
end

#compactObject



181
182
183
# File 'lib/typed_array.rb', line 181

def compact
  TypedArray.new(item_class).concat(super)
end

#concat(items) ⇒ Object



45
46
47
48
# File 'lib/typed_array.rb', line 45

def concat(items)
  validate_assigned_items(items)
  super
end

#drop(*args) ⇒ Object



217
218
219
# File 'lib/typed_array.rb', line 217

def drop(*args)
  TypedArray.new(item_class).concat(super)
end

#drop_while(&block) ⇒ Object



221
222
223
224
225
226
227
# File 'lib/typed_array.rb', line 221

def drop_while(&block)
  if block_given?
    TypedArray.new(item_class).concat(super)
  else
    super
  end
end

#eql?(item) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/typed_array.rb', line 23

def eql?(item)
  item.is_a?(TypedArray) && item_class == item.item_class && super
end

#fill(*args, &block) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/typed_array.rb', line 131

def fill(*args, &block)
  if block_given?
    super(*args) do |index|
      value = block.call(index)
      validate_assigned_items([value])
      value
    end
  else
    validate_assigned_items([args.first])
    super
  end
end

#insert(index, *items) ⇒ Object



55
56
57
58
# File 'lib/typed_array.rb', line 55

def insert(index, *items)
  validate_assigned_items(items)
  super
end

#pop(*args) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/typed_array.rb', line 70

def pop(*args)
  if args.size > 0
    TypedArray.new(item_class).concat(super)
  else
    super
  end
end

#product(*args) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/typed_array.rb', line 197

def product(*args)
  if args.all? { |items| items.is_a?(TypedArray) && items.item_class == item_class }
    super.map { |items| TypedArray.new(item_class).concat(items) }
  else
    super
  end
end

#push(*items) ⇒ Object



60
61
62
63
# File 'lib/typed_array.rb', line 60

def push(*items)
  validate_assigned_items(items)
  super
end

#reject(&block) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/typed_array.rb', line 118

def reject(&block)
  if block_given?
    TypedArray.new(item_class).concat(super)
  else
    super
  end
end

#replace(items) ⇒ Object



126
127
128
129
# File 'lib/typed_array.rb', line 126

def replace(items)
  validate_assigned_items(items)
  super
end

#reverseObject



86
87
88
# File 'lib/typed_array.rb', line 86

def reverse
  TypedArray.new(item_class).concat(super)
end

#rotate(count = 1) ⇒ Object



90
91
92
# File 'lib/typed_array.rb', line 90

def rotate(count=1)
  TypedArray.new(item_class).concat(super)
end

#sample(*args) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/typed_array.rb', line 189

def sample(*args)
  if args.size > 0 && !args.first.is_a?(Hash)
    TypedArray.new(item_class).concat(super)
  else
    super
  end
end

#select(&block) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/typed_array.rb', line 106

def select(&block)
  if block_given?
    TypedArray.new(item_class).concat(super)
  else
    super
  end
end

#shift(*args) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/typed_array.rb', line 78

def shift(*args)
  if args.size > 0
    TypedArray.new(item_class).concat(super)
  else
    super
  end
end

#shuffle(*args) ⇒ Object



185
186
187
# File 'lib/typed_array.rb', line 185

def shuffle(*args)
  TypedArray.new(item_class).concat(super)
end

#slice(*args) ⇒ Object



144
145
146
# File 'lib/typed_array.rb', line 144

def slice(*args)
  copy_item_class(super)
end

#slice!(*args) ⇒ Object



148
149
150
# File 'lib/typed_array.rb', line 148

def slice!(*args)
  copy_item_class(super)
end

#sort(*args) ⇒ Object



94
95
96
# File 'lib/typed_array.rb', line 94

def sort(*args)
  TypedArray.new(item_class).concat(super)
end

#sort_by(&block) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/typed_array.rb', line 98

def sort_by(&block)
  if block_given?
    TypedArray.new(item_class).concat(super)
  else
    super
  end
end

#take(*args) ⇒ Object



205
206
207
# File 'lib/typed_array.rb', line 205

def take(*args)
  TypedArray.new(item_class).concat(super)
end

#take_while(&block) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/typed_array.rb', line 209

def take_while(&block)
  if block_given?
    TypedArray.new(item_class).concat(super)
  else
    super
  end
end

#uniq(&block) ⇒ Object



177
178
179
# File 'lib/typed_array.rb', line 177

def uniq(&block)
  copy_item_class(super)
end

#unshift(*items) ⇒ Object



65
66
67
68
# File 'lib/typed_array.rb', line 65

def unshift(*items)
  validate_assigned_items(items)
  super
end

#values_at(*args) ⇒ Object



114
115
116
# File 'lib/typed_array.rb', line 114

def values_at(*args)
  TypedArray.new(item_class).concat(super)
end

#|(items) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/typed_array.rb', line 169

def |(items)
  if items.is_a?(TypedArray) && items.item_class == item_class
    TypedArray.new(item_class).concat(super)
  else
    super
  end
end