Class: Array
- Defined in:
- lib/reactive_support/core_ext/object/blank.rb,
lib/reactive_support/core_ext/array/access.rb,
lib/reactive_support/core_ext/object/deep_dup.rb,
lib/reactive_support/core_ext/array/extract_options.rb
Overview
Instance Method Summary collapse
-
#deep_dup ⇒ Object
When called on an array, the
#deep_dupmethod duplicates all the members of the array recursively, so that actions on the duplicate do not affect the original:. -
#extract_options! ⇒ Object
The
#extract_options!method retrieves a hash of options from the end of a set of splat args. -
#from(position) ⇒ Object
The
#frommethod returns the tail of the array starting at the givenposition. -
#present? ⇒ Boolean
When called on an array, the
#present?method returnstrueif the array has any elements. -
#to(position) ⇒ Object
The
#tomethod returns the beginning of the array up to and including the givenposition.
Instance Method Details
#deep_dup ⇒ Object
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]]
36 37 38 |
# File 'lib/reactive_support/core_ext/object/deep_dup.rb', line 36 def deep_dup self.map {|item| item.deep_dup } end |
#extract_options! ⇒ Object
The #extract_options! method retrieves a hash of options from the end of a set of splat args. If the last element of the set is Hash or instance of another class implementing the #extractable_options? method, #extract_options! method pops and returns that object. Otherwise, it returns an empty Hash.
22 23 24 |
# File 'lib/reactive_support/core_ext/array/extract_options.rb', line 22 def return last.is_a?(Hash) && last. ? pop : {} 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
84 85 86 |
# File 'lib/reactive_support/core_ext/object/blank.rb', line 84 def present? !blank? 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 |