Class: Array
- Defined in:
- lib/simple_ext/array/access.rb,
lib/simple_ext/array/values.rb,
lib/simple_ext/object/blank.rb,
lib/simple_ext/object/to_query.rb
Instance Method Summary collapse
-
#any_blank? ⇒ Boolean
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.
-
#any_empty? ⇒ Boolean
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.
-
#any_nil? ⇒ Boolean
Check if any element is nil in array [1, 2, ‘ ’, 4].any_nil? => false [1, nil, 2, 4].any_nil? => true.
-
#excluding(*elements) ⇒ Object
Returns a copy of the Array excluding the specified elements.
-
#extract! ⇒ Object
Removes and returns the elements for which the block returns a true value.
-
#fifth ⇒ Object
Equal to
self[4]
. -
#find_hash_with(arg) ⇒ Object
Find a hash from array which matches given arg arg: should be hash.
-
#forty_two ⇒ Object
Equal to
self[41]
. -
#fourth ⇒ Object
Equal to
self[3]
. -
#from(position) ⇒ Object
Returns the tail of the array from
position
. -
#include_hash_with?(arg) ⇒ Boolean
Check if array contains a hash with given arg arg: should be an hash.
-
#include_string_with?(arg) ⇒ Boolean
Check if array contains a string with given substring.
-
#including(*elements) ⇒ Object
Returns a new array that includes the passed elements.
-
#second ⇒ Object
Equal to
self[1]
. -
#second_to_last ⇒ Object
Equal to
self[-2]
. -
#select_hash_with(arg) ⇒ Object
Select hashes from array, which matches given arg arg: should be hash.
-
#third ⇒ Object
Equal to
self[2]
. -
#third_to_last ⇒ Object
Equal to
self[-3]
. -
#to(position) ⇒ Object
Returns the beginning of the array up to
position
. -
#to_param ⇒ Object
Calls
to_param
on all its elements and joins the result with slashes. -
#to_query(key) ⇒ Object
Converts an array into a string suitable for use as a URL query string, using the given
key
as the param name. -
#without(*elements) ⇒ Object
Alias for #excluding.
Instance Method Details
#any_blank? ⇒ Boolean
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
28 29 30 |
# File 'lib/simple_ext/array/values.rb', line 28 def any_blank? any?(&:blank?) end |
#any_empty? ⇒ Boolean
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
18 19 20 |
# File 'lib/simple_ext/array/values.rb', line 18 def any_empty? any? { |e| e.respond_to?(:empty?) && e.empty? } end |
#any_nil? ⇒ Boolean
Check if any element is nil in array [1, 2, ‘ ’, 4].any_nil? => false [1, nil, 2, 4].any_nil? => true
9 10 11 |
# File 'lib/simple_ext/array/values.rb', line 9 def any_nil? any?(&:nil?) end |
#excluding(*elements) ⇒ Object
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.
65 66 67 |
# File 'lib/simple_ext/array/access.rb', line 65 def excluding(*elements) self - elements.flatten(1) end |
#extract! ⇒ Object
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]
10 11 12 13 14 15 16 17 18 19 20 |
# 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 ⇒ Object
Equal to self[4]
.
%w( a b c d e ).fifth # => "e"
98 99 100 |
# File 'lib/simple_ext/array/access.rb', line 98 def fifth self[4] end |
#find_hash_with(arg) ⇒ Object
Find a hash from array which matches given arg arg: should be hash
arr = [1, b: 2, c: 3, 4, b: 5, c: 6] arr.find_hash_with(4) => 4, b: 5, c: 6 arr.find_hash_with(=> 2, “c” => 3) => 1, b: 2, c: 3 arr.find_hash_with({ a: 4, c: 8 }) => nil
62 63 64 |
# 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 ⇒ Object
Equal to self[41]
. Also known as accessing “the reddit”.
(1..42).to_a.forty_two # => 42
105 106 107 |
# File 'lib/simple_ext/array/access.rb', line 105 def forty_two self[41] end |
#fourth ⇒ Object
Equal to self[3]
.
%w( a b c d e ).fourth # => "d"
91 92 93 |
# File 'lib/simple_ext/array/access.rb', line 91 def fourth self[3] end |
#from(position) ⇒ Object
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) # => []
30 31 32 |
# File 'lib/simple_ext/array/access.rb', line 30 def from(position) self[position, length] || [] end |
#include_hash_with?(arg) ⇒ Boolean
Check if array contains a hash with given arg arg: should be an hash
arr = [1, b: 2, c: 3, 4, b: 5, c: 6] arr.include_hash_with?(4) => true arr.include_hash_with?(=> 4, “b” => 5) => true arr.include_hash_with?(4, b: 6) => false
40 41 42 |
# File 'lib/simple_ext/array/values.rb', line 40 def include_hash_with?(arg) !find_hash_with(arg).blank? end |
#include_string_with?(arg) ⇒ Boolean
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
50 51 52 |
# File 'lib/simple_ext/array/values.rb', line 50 def include_string_with?(arg) any? { |e| e.include?(arg) } end |
#including(*elements) ⇒ Object
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 ] ]
54 55 56 |
# File 'lib/simple_ext/array/access.rb', line 54 def including(*elements) self + elements.flatten(1) end |
#second ⇒ Object
Equal to self[1]
.
%w( a b c d e ).second # => "b"
77 78 79 |
# File 'lib/simple_ext/array/access.rb', line 77 def second self[1] end |
#second_to_last ⇒ Object
Equal to self[-2]
.
%w( a b c d e ).second_to_last # => "d"
119 120 121 |
# File 'lib/simple_ext/array/access.rb', line 119 def second_to_last self[-2] end |
#select_hash_with(arg) ⇒ Object
Select hashes from array, which matches given arg arg: should be hash
arr = [1, b: 2, c: 3, 4, b: 5, c: 6, 1, b: 8, c: 9] arr.select_hash_with({ a: 1 }) => [1, b: 2, c: 3, 1, b: 8, c: 9]
72 73 74 |
# 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 ⇒ Object
Equal to self[2]
.
%w( a b c d e ).third # => "c"
84 85 86 |
# File 'lib/simple_ext/array/access.rb', line 84 def third self[2] end |
#third_to_last ⇒ Object
Equal to self[-3]
.
%w( a b c d e ).third_to_last # => "c"
112 113 114 |
# File 'lib/simple_ext/array/access.rb', line 112 def third_to_last self[-3] end |
#to(position) ⇒ Object
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) # => []
42 43 44 45 46 47 48 |
# 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 ⇒ Object
Calls to_param
on all its elements and joins the result with slashes. This is used by url_for
in Action Pack.
42 43 44 |
# File 'lib/simple_ext/object/to_query.rb', line 42 def to_param collect(&:to_param).join "/" end |
#to_query(key) ⇒ Object
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"
50 51 52 53 54 55 56 57 58 |
# 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) ⇒ Object
Alias for #excluding.
70 71 72 |
# File 'lib/simple_ext/array/access.rb', line 70 def without(*elements) excluding(*elements) end |