class Array
Add extensions to the default class(String).
Public Instance Methods
@param [Array] ending
@raise [ArgumentError]
@return [Boolean] true, if this array ends with the elements in the provided parameter array (order-matters: true, duplicates-matter: true)
# File lib/ruuuby/extensions.rb, line 135 def end_with?(ending) Ruuuby::Params::check_array(ending) if ending.length == 0 self.length == 0 else self[(self.length-ending.length)..(self.length-1)] == ending[(0)..(ending.length-1)] end end
@param [Array] ending
@raise [ArgumentError]
@return [Array]
# File lib/ruuuby/extensions.rb, line 149 def ensure_ending!(ending) Ruuuby::Params::check_array(ending) len_ending = ending.length return self if len_ending == 0 || self.end_with?(ending) len_this = self.length return self << ending[0] if len_ending == 1 if len_this == 0 ending.each { |element| self << element } return self end delta = 0 last_matched = nil while delta <= len_this && delta <= len_ending ending_of_this = self[(len_this-1-delta)..(len_this-1)] starting_of_end = ending[0..delta] last_matched = starting_of_end if ending_of_this == starting_of_end delta += 1 end if last_matched == nil ending.each { |element| self << element } else ending[(last_matched.length)..(len_ending-1)].each { |element| self << element } end self end
@param [Array] them
@raise [ArgumentError]
@return [Boolean] true, if this array and provided array both have same content (order-matters: false, duplicates-matter: false)
# File lib/ruuuby/extensions.rb, line 123 def equal_contents?(them) Ruuuby::Params::check_array(them) return true if (self.length == 0 && them.length == 0) return false if (self.length != them.length) uniq_to_me(them).empty? && uniq_to_them(them).empty? end
@return [Array]
# File lib/ruuuby/extensions.rb, line 113 def remove_empty! return self if self.length == 0 compact!.reject!{|element| element.respond_to?('empty?') && element.empty?} end
@param [Array] them | another array to be compared against
@raise [ArgumentError]
@return [Array] a new array instance containing values only appearing in base array (and not in provided array)
# File lib/ruuuby/extensions.rb, line 87 def uniq_to_me(them) Ruuuby::Params::check_array(them) self.uniq - them.uniq end
@param [Array] them | an array to be compared against
@raise [ArgumentError]
@return [Array] a new array instance containing values only appearing in provided array (and not in base array)
# File lib/ruuuby/extensions.rb, line 97 def uniq_to_them(them) Ruuuby::Params::check_array(them) them.uniq - self.uniq end