Class: GoogleMaps::Services::ArrayBox

Inherits:
Object
  • Object
show all
Defined in:
lib/googlemaps/services/util.rb

Overview

Performs Array boxing.

Class Method Summary collapse

Class Method Details

.contains_all?(arr, other) ⇒ TrueClass, FalseClass

Determines if one array contains all elements of another array.

Examples:

myarr = ["hello", "world"]
ArrayBox.contains_all? myarr, ["hello"] # true

Parameters:

  • arr (Array)

    target array.

  • other (Array)

    array to look for in the target array.

Returns:

  • (TrueClass, FalseClass)

    a boolean.



51
52
53
54
55
56
57
58
59
# File 'lib/googlemaps/services/util.rb', line 51

def self.contains_all?(arr, other)
  h = arr.inject(Hash.new(0)) {|h, i| h[i] += 1; h}
  other.each do |i|
    return false unless h.has_key?(i)
    return false if h[i].zero?
    h[i] -= 1
  end
  return true
end

.wrap(object) ⇒ Array

Wrap its argument in an array unless it is already an array or (array-like).

Examples:

Wrap any Object in a array

ArrayBox.wrap(nil)       # []
ArrayBox.wrap([1, 2, 3]) # [1, 2, 3]
ArrayBox.wrap(1)         # [1]

Parameters:

  • object (Object)

    Object to wrap.

Returns:

  • (Array)

    an array.



32
33
34
35
36
37
38
39
40
# File 'lib/googlemaps/services/util.rb', line 32

def self.wrap(object)
  if object.nil?
    []
  elsif object.respond_to? :to_ary
    object.to_ary || [object]
  else
    [object]
  end
end