Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/mimi/core/core_ext.rb
Instance Method Summary collapse
-
#deep_dup ⇒ Hash
Duplicates an Array with all nested values.
-
#except(*keys) ⇒ Array
Returns an Array with given values excluded, if present.
-
#except!(*keys) ⇒ Array
Modifies the Array excluding given values, if present.
-
#only(*values) ⇒ Array
Returns an Array with only selected values, if present.
-
#only!(*values) ⇒ Array
Modifies the Array keeping only given values, if present.
Instance Method Details
#deep_dup ⇒ Hash
Duplicates an Array with all nested values
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
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
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
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
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 |