Class: Array

Inherits:
Object show all
Defined in:
lib/reactive_support/core_ext/object/blank.rb,
lib/extensions/array_extensions.rb,
lib/reactive_support/core_ext/array/access.rb,
lib/reactive_support/core_ext/object/deep_dup.rb

Overview

Ruby’s core Array class. See documentation for version 2.1.3, 2.0.0, or 1.9.3.

Instance Method Summary collapse

Instance Method Details

#deep_dupObject

When called on an array, the #deep_dup method duplicates all the members of the array recursively, so that actions on the duplicate do not affect the original:

arr = [1, 2, [3, 4]]                  # => [1, 2, [3, 4]]
dup, deep = arr.dup, arr.deep_dup     # => [1, 2, [3, 4]], [1, 2, [3, 4]]

deep[2][1] = 5                        # => [1, 2, [3, 5]]
p arr                                 # => [1, 2, [3, 4]]

dup[2][1] = 5                         # => [1, 2, [3, 5]]
p arr                                 # => [1, 2, [3, 5]]


34
35
36
# File 'lib/reactive_support/core_ext/object/deep_dup.rb', line 34

def deep_dup
  self.map {|item| item.deep_dup }
end

#from(position) ⇒ Object

The #from method returns the tail of the array starting at the given position.

[1, 2, 3, 4, 5].from(2)     # => [3, 4, 5]
[1, 2, 3, 4, 5].from(0)     # => [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5].from(-2)    # => [4, 5]

#from returns an empty array if the receiving array is empty, the given position exceeds the maximum index of the array, or the given position is lower than the array’s minimum (i.e., largest negative) index.

[].from(0)                  # => []
[1, 2, 3, 4, 5].from(10)    # => []
[1, 2, 3, 4, 5].from(-10)   # => []


16
17
18
# File 'lib/reactive_support/core_ext/array/access.rb', line 16

def from(position)
  self[position, length] ||  []
end

#present?Boolean

When called on an array, the #present? method returns true if the array has any elements. Note that #present? also returns true if the array consists of blank values:

['foo', 'bar'].present?    # => true 
[false, nil].present?      # => true 
[].present?                # => false

Returns:

  • (Boolean)


79
80
81
# File 'lib/reactive_support/core_ext/object/blank.rb', line 79

def present?
  !blank?
end

#scope(key, *values) ⇒ Object

The #scope method is called on an array of hashes. It returns a sub-array including only hashes for which the value at a given key equals the given value. The #scope method is non-destructive; the original array will remain intact after it is called. The #scope method is known to work for string or symbol keys. It should work for other data type keys as well.

Example:

array = [
  { name: 'Jean-Paul Sartre', nationality: 'French' },
  { name: 'Bertrand Russell', nationality: 'English' },
  { name: 'Ludwig Wittgenstein', nationality: 'Austrian' },
  { name: 'Albert Camus', nationality: 'French' }
]

array.scope(:nationality, 'French')
  # => [
         { name: 'Jean-Paul Sartre', nationality: 'French' },
         { name: 'Albert Camus', nationality: 'French' }
       ]


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

def scope(key, *values)
  self.select {|hash| hash[key].in?(values) }
end

#to(position) ⇒ Object

The #to method returns the beginning of the array up to and including the given position.

[1, 2, 3, 4, 5].to(2)       # => [1, 2, 3]
[1, 2, 3, 4, 5].to(10)      # => [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5].to(-2)      # => [1, 2, 3, 4]

#to returns an empty array if the receiving array is empty or the given position falls below the minimum (negative) index.

[].to(0)                    # => []
[1, 2, 3, 4, 5].to(-10)     # => []


31
32
33
# File 'lib/reactive_support/core_ext/array/access.rb', line 31

def to(position)
  self[0..position]
end

#where_not(key, *values) ⇒ Object

The #where_not method is called on an array of hashes. It returns a sub-array including only hashes for which the value at a given key does not equal the given value. It is the inverse of the #scope method. The #where_not method is non-destructive; the original array will remain intact after it is called. The #where_not method is known to work for string or symbol keys. It should work for other data types as well.

Example:

array = [
  { name: 'Jean-Paul Sartre', nationality: 'French' },
  { name: 'Bertrand Russell', nationality: 'English' },
  { name: 'Ludwig Wittgenstein', nationality: 'Austrian' },
  { name: 'Albert Camus', nationality: 'French' }
]

array.where_not(:nationality, 'French')
  # => [
         { name: 'Bertrand Russell', nationality: 'English' },
         { name: 'Ludwig Wittgenstein', nationality: 'English' }
       ]


48
49
50
# File 'lib/extensions/array_extensions.rb', line 48

def where_not(key, *values)
  self.reject {|hash| hash[key].in?(values) }
end