Class: Array
Instance Method Summary collapse
-
#contains?(klass) ⇒ Boolean
Check if the array contains any instances of a specific class.
-
#flipflop ⇒ Object
Thanks to manveru for this fun code :) All it does is flip the first and last elements.
-
#flipflop! ⇒ Object
Destructive version of Array#flipflop.
-
#nothing? ⇒ Boolean
Similar to String#nothing?, except it joins all the elements first and does the same check.
-
#replace_array(match_array, replace_arry) ⇒ Object
replaces matched array
match_arrayasreplace_arrayif the array contains all elements and with same sort.
Instance Method Details
#contains?(klass) ⇒ Boolean
Check if the array contains any instances of a specific class.
Example: ['foo', 1, :bar].contains? Symbol #=> true
43 44 45 |
# File 'lib/extra_lib/core_ext/array.rb', line 43 def contains?(klass) map { |obj| obj.class }.include? klass end |
#flipflop ⇒ Object
Thanks to manveru for this fun code :) All it does is flip the first and last elements. Pretty cool, eh? :)
Example: [1, 2, 3, 4].flipflop #=> [4, 2, 3, 1]
Returns: Array
9 10 11 12 13 14 15 |
# File 'lib/extra_lib/core_ext/array.rb', line 9 def flipflop if size > 1 [last] + self[1...-1] + [first] else self end end |
#flipflop! ⇒ Object
Destructive version of Array#flipflop.
Returns: Array or nil
21 22 23 24 25 26 |
# File 'lib/extra_lib/core_ext/array.rb', line 21 def flipflop! if size > 1 a, b = shift, pop unshift(b); push(a) end end |
#nothing? ⇒ Boolean
Similar to String#nothing?, except it joins all the elements first and does the same check.
Example: <tt>[“ ”, “ ”, “”].nothing? #=> true<tt>
Returns: True or false.
35 36 37 |
# File 'lib/extra_lib/core_ext/array.rb', line 35 def nothing? join('').strip.empty? end |
#replace_array(match_array, replace_arry) ⇒ Object
replaces matched array match_array as replace_array if the array contains all elements and with same sort
Example: [1, 5, 3, 4, 2].replace_array([5,3],["3", "5"]) #=> [1, "3", "5", 4, 2]
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/extra_lib/core_ext/array.rb', line 51 def replace_array(match_array, replace_arry) array = [] match_array self.each_index{|i| if self[i].eql?(match_array.first) and self.slice(i, match_array.count).eql?(match_array) array.concat self.first(i) array.concat replace_arry array.concat self.drop(i + match_array.count) break end } array = self if array.empty? array end |