class Array

Public Instance Methods

contains?(klass) click to toggle source

Check if the array contains any instances of a specific class.

Example: ['foo', 1, :bar].contains? Symbol #=> true

# File lib/extra_lib/core_ext/array.rb, line 43
def contains?(klass)
        map { |obj| obj.class }.include? klass
end
flipflop() click to toggle source

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

# 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!() click to toggle source

Destructive version of Array#flipflop.

Returns: Array or nil

# 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?() click to toggle source

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.

# File lib/extra_lib/core_ext/array.rb, line 35
def nothing?
        join('').strip.empty?
end
replace_array(match_array, replace_arry) click to toggle source

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]

# 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