Class: Generics::List::Mutable

Inherits:
Generics::List show all
Defined in:
lib/generics/collections/list.rb

Instance Method Summary collapse

Methods inherited from Generics::List

[], #[], #compatible!, #compatible?, #each, #empty?, #inspect, #last, #length, #map, #pop, #qdup, #reverse, #sample, #shift, #size, #slice, #to_s, type, #uniq

Constructor Details

#initialize(*collection) ⇒ Mutable

Returns a new instance of Mutable

Parameters:

  • type (Class, Module)

168
169
170
171
# File 'lib/generics/collections/list.rb', line 168

def initialize(*collection)
  collection.each { |value| compatible!(value) }
  @collection = collection.freeze
end

Instance Method Details

#[]=(index, value) ⇒ Object


201
202
203
204
# File 'lib/generics/collections/list.rb', line 201

def []=(index, value)
  compatible!(value)
  @collection[index] = value
end

#add(value) ⇒ Object

Destructive


187
188
189
190
# File 'lib/generics/collections/list.rb', line 187

def add(value)
  compatible!(value)
  @collection.push(value)
end

#cloneList::Mutable

Preserves frozen state and does copies singleton state

Returns:


181
182
183
# File 'lib/generics/collections/list.rb', line 181

def clone
  self.class.new(*@collection.clone)
end

#delete(value) ⇒ Object


211
212
213
214
# File 'lib/generics/collections/list.rb', line 211

def delete(value)
  compatible!(value)
  @collection.delete(value)
end

#delete_at(index) ⇒ Object


216
217
218
# File 'lib/generics/collections/list.rb', line 216

def delete_at(index)
  @collection.delete_at(index)
end

#dupList::Mutable

Does not preserve frozen state and does not copy singleton state

Returns:


175
176
177
# File 'lib/generics/collections/list.rb', line 175

def dup
  self.class.new(*@collection.dup)
end

#insert(index, *values) ⇒ Object


206
207
208
209
# File 'lib/generics/collections/list.rb', line 206

def insert(index, *values)
  values.each { |value| compatible!(value) }
  self.class.new(*@collection.insert(index, *values))
end

#set(index, value, &_block) ⇒ Object


192
193
194
195
196
197
198
199
# File 'lib/generics/collections/list.rb', line 192

def set(index, value, &_block)
  compatible!(value)
  if block_given?
    @collection[index] = yield @collection[index]
  else
    @collection[index] = value
  end
end

#unshift(value) ⇒ Object


220
221
222
223
# File 'lib/generics/collections/list.rb', line 220

def unshift(value)
  compatible!(value)
  self.class.new(*@collection.unshift(value))
end