Class: Array
- Defined in:
- lib/simple_ext/array/access.rb,
lib/simple_ext/object/blank.rb,
lib/simple_ext/object/to_query.rb
Instance Method Summary collapse
-
#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]. -
#forty_two ⇒ Object
Equal to
self[41]. -
#fourth ⇒ Object
Equal to
self[3]. -
#from(position) ⇒ Object
Returns the tail of the array from
position. -
#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]. -
#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_paramon 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
keyas the param name. -
#without(*elements) ⇒ Object
Alias for #excluding.
Instance Method Details
#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 |
#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 |
#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 |
#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 |