Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/mimi/core/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#deep_dupHash

Duplicates an Array with all nested values

Returns:

  • (Hash)

    a new Hash



296
297
298
299
300
# File 'lib/mimi/core/core_ext.rb', line 296

def deep_dup
  map do |v|
    v.respond_to?(:deep_dup) ? v.deep_dup : v.dup
  end
end

#except(*keys) ⇒ Array

Returns an Array with given values excluded, if present

Examples:

a = [:a, :b, :c]
a.except(:a, :b, :d) # => [:c]

Parameters:

  • *values (*)

    list of values

Returns:

  • (Array)

    a new Array



266
267
268
# File 'lib/mimi/core/core_ext.rb', line 266

def except(*keys)
  dup.except!(*keys)
end

#except!(*keys) ⇒ Array

Modifies the Array excluding given values, if present

Examples:

a = [:a, :b, :c]
a.except!(:a, :b, :d)
a # => [:c]

Parameters:

  • *values (*)

    list of values

Returns:



280
281
282
283
284
285
286
287
# File 'lib/mimi/core/core_ext.rb', line 280

def except!(*keys)
  if keys.size == 1 && keys.first.is_a?(Array)
    raise ArgumentError, 'Array#except!() expects keys as list of arguments,' \
      ' not an Array as first argument'
  end
  reject! { |k| keys.include?(k) }
  self
end

#only(*values) ⇒ Array

Returns an Array with only selected values, if present

Examples:

a = [:a, :b, :c]
a.only(:a, :b, :d) # => [:a, :b]

Parameters:

  • *values (*)

    list of values

Returns:

  • (Array)

    a new Array



231
232
233
# File 'lib/mimi/core/core_ext.rb', line 231

def only(*values)
  dup.only!(*values)
end

#only!(*values) ⇒ Array

Modifies the Array keeping only given values, if present

Examples:

a = [:a, :b, :c]
a.only!(:a, :b, :d)
a # => [:a, :b]

Parameters:

  • *values (*)

    list of values

Returns:



245
246
247
248
249
250
251
252
# File 'lib/mimi/core/core_ext.rb', line 245

def only!(*values)
  if values.size == 1 && values.first.is_a?(Array)
    raise ArgumentError, 'Array#only!() expects values as list of arguments,' \
      ' not an Array as first argument'
  end
  select! { |k| values.include?(k) }
  self
end