class Array

Public Instance Methods

any_blank?() click to toggle source

Check if any element is blank in array [1, 2, ' ', 4].any_blank? => true [1, nil, 2, 4].any_blank? => true [1, '', 3, 4].any_blank? => true [1, 2, 3, 4].any_blank? => false

# File lib/simple_ext/array/values.rb, line 28
def any_blank?
  any?(&:blank?)
end
any_empty?() click to toggle source

Check if any element is empty in array [1, 2, ' ', 4].any_empty? => false [1, nil, 2, 4].any_empty? => false [1, '', 3, 4].any_empty? => true

# File lib/simple_ext/array/values.rb, line 18
def any_empty?
  any? { |e| e.respond_to?(:empty?) && e.empty? }
end
any_nil?() click to toggle source

Check if any element is nil in array [1, 2, ' ', 4].any_nil? => false [1, nil, 2, 4].any_nil? => true

# File lib/simple_ext/array/values.rb, line 9
def any_nil?
  any?(&:nil?)
end
excluding(*elements) click to toggle source

Returns a copy of the Array excluding the specified elements.

["David", "Rafael", "Aaron", "Todd"].excluding("Aaron", "Todd") # => ["David", "Rafael"]
[ [ 0, 1 ], [ 1, 0 ] ].excluding([ [ 1, 0 ] ]) # => [ [ 0, 1 ] ]

Note: This is an optimization of Enumerable#excluding that uses Array#- instead of Array#reject for performance reasons.

# File lib/simple_ext/array/access.rb, line 65
def excluding(*elements)
  self - elements.flatten(1)
end
extract!() { |element| ... } click to toggle source

Removes and returns the elements for which the block returns a true value. If no block is given, an Enumerator is returned instead.

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
odd_numbers = numbers.extract! { |number| number.odd? } # => [1, 3, 5, 7, 9]
numbers # => [0, 2, 4, 6, 8]
# File lib/simple_ext/array/access.rb, line 10
def extract!
  return to_enum(:extract!) { size } unless block_given?

  extracted_elements = []

  reject! do |element|
    extracted_elements << element if yield(element)
  end

  extracted_elements
end
fifth() click to toggle source

Equal to self[4].

%w( a b c d e ).fifth # => "e"
# File lib/simple_ext/array/access.rb, line 98
def fifth
  self[4]
end
find_hash_with(arg) click to toggle source

Find a hash from array which matches given arg arg: should be hash

arr = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}] arr.find_hash_with({a: 4}) => {a: 4, b: 5, c: 6} arr.find_hash_with({“b” => 2, “c” => 3}) => {a: 1, b: 2, c: 3} arr.find_hash_with({ a: 4, c: 8 }) => nil

# File lib/simple_ext/array/values.rb, line 62
def find_hash_with(arg)
  find { |h| h.stringify_keys.sub_hash?(arg.stringify_keys) }
end
forty_two() click to toggle source

Equal to self[41]. Also known as accessing “the reddit”.

(1..42).to_a.forty_two # => 42
# File lib/simple_ext/array/access.rb, line 105
def forty_two
  self[41]
end
fourth() click to toggle source

Equal to self[3].

%w( a b c d e ).fourth # => "d"
# File lib/simple_ext/array/access.rb, line 91
def fourth
  self[3]
end
from(position) click to toggle source

Returns the tail of the array from position.

%w( a b c d ).from(0)  # => ["a", "b", "c", "d"]
%w( a b c d ).from(2)  # => ["c", "d"]
%w( a b c d ).from(10) # => []
%w().from(0)           # => []
%w( a b c d ).from(-2) # => ["c", "d"]
%w( a b c ).from(-10)  # => []
# File lib/simple_ext/array/access.rb, line 30
def from(position)
  self[position, length] || []
end
include_hash_with?(arg) click to toggle source

Check if array contains a hash with given arg arg: should be an hash

arr = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}] arr.include_hash_with?({a: 4}) => true arr.include_hash_with?({“a” => 4, “b” => 5}) => true arr.include_hash_with?({a: 4, b: 6}) => false

# File lib/simple_ext/array/values.rb, line 40
def include_hash_with?(arg)
  !find_hash_with(arg).blank?
end
include_string_with?(arg) click to toggle source

Check if array contains a string with given substring

arr = ['abc', 'def', 'pqr', 'xyz'] arr.include_string_with?('pq') => true arr.include_string_with?('df') => false

# File lib/simple_ext/array/values.rb, line 50
def include_string_with?(arg)
  any? { |e| e.include?(arg) }
end
including(*elements) click to toggle source

Returns a new array that includes the passed elements.

[ 1, 2, 3 ].including(4, 5) # => [ 1, 2, 3, 4, 5 ]
[ [ 0, 1 ] ].including([ [ 1, 0 ] ]) # => [ [ 0, 1 ], [ 1, 0 ] ]
# File lib/simple_ext/array/access.rb, line 54
def including(*elements)
  self + elements.flatten(1)
end
second() click to toggle source

Equal to self[1].

%w( a b c d e ).second # => "b"
# File lib/simple_ext/array/access.rb, line 77
def second
  self[1]
end
second_to_last() click to toggle source

Equal to self[-2].

%w( a b c d e ).second_to_last # => "d"
# File lib/simple_ext/array/access.rb, line 119
def second_to_last
  self[-2]
end
select_hash_with(arg) click to toggle source

Select hashes from array, which matches given arg arg: should be hash

arr = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}, {a: 1, b: 8, c: 9}] arr.select_hash_with({ a: 1 }) => [{a: 1, b: 2, c: 3}, {a: 1, b: 8, c: 9}]

# File lib/simple_ext/array/values.rb, line 72
def select_hash_with(arg)
  select { |h| h.stringify_keys.sub_hash?(arg.stringify_keys) }
end
third() click to toggle source

Equal to self[2].

%w( a b c d e ).third # => "c"
# File lib/simple_ext/array/access.rb, line 84
def third
  self[2]
end
third_to_last() click to toggle source

Equal to self[-3].

%w( a b c d e ).third_to_last # => "c"
# File lib/simple_ext/array/access.rb, line 112
def third_to_last
  self[-3]
end
to(position) click to toggle source

Returns the beginning of the array up to position.

%w( a b c d ).to(0)  # => ["a"]
%w( a b c d ).to(2)  # => ["a", "b", "c"]
%w( a b c d ).to(10) # => ["a", "b", "c", "d"]
%w().to(0)           # => []
%w( a b c d ).to(-2) # => ["a", "b", "c"]
%w( a b c ).to(-10)  # => []
# File lib/simple_ext/array/access.rb, line 42
def to(position)
  if position >= 0
    take position + 1
  else
    self[0..position]
  end
end
to_param() click to toggle source

Calls to_param on all its elements and joins the result with slashes. This is used by url_for in Action Pack.

# File lib/simple_ext/object/to_query.rb, line 42
def to_param
  collect(&:to_param).join "/"
end
to_query(key) click to toggle source

Converts an array into a string suitable for use as a URL query string, using the given key as the param name.

['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
# File lib/simple_ext/object/to_query.rb, line 50
def to_query(key)
  prefix = "#{key}[]"

  if empty?
    nil.to_query(prefix)
  else
    collect { |value| value.to_query(prefix) }.join "&"
  end
end
without(*elements) click to toggle source

Alias for excluding.

# File lib/simple_ext/array/access.rb, line 70
def without(*elements)
  excluding(*elements)
end