class Array

Public Instance Methods

bury(where, value) click to toggle source

Stores a value on the location indicated input:

where: (Array)
value

examples:

my_array.bury([3, 0], "doom") # array of array
my_array.bury([2, 1, :original],"the value to set") #array of array of hash
# File lib/nice/hash/add_to_ruby.rb, line 80
def bury(where, value)
  me = self
  where[0..-2].each do |key|
    me = me[key]
  end
  me[where[-1]] = value
end
json(*keys) click to toggle source

In case of an array of json strings will return the keys specified. The keys need to be provided as symbols input:

keys:
   1 value with key or an array of keys
     In case the key supplied doesn't exist in the hash then it will be return nil for that one

output:

if keys given: a hash of (keys, values) or the value, if the key is found more than once in the json string, then it will be return a hash of arrays
if no keys given, an empty hash
# File lib/nice/hash/add_to_ruby.rb, line 98
def json(*keys)
  json_string = "[#{join(",")}]"
  json_string.json(*keys)
end
method_missing(m, *arguments, &block) click to toggle source

For Array of Hashes returns an array of values of the hash key specified in case doesn't exist an Array method with the same name The keys can be accessed also adding underscore to avoid problems with existent methods examples for the array of hashes [{name: 'Peter', city: 'Madrid'}, {name: 'Lola', city: 'NYC'}] :

my_array.city
my_array._name
Calls superclass method
# File lib/nice/hash/add_to_ruby.rb, line 334
def method_missing(m, *arguments, &block)
  m = m[1..-1].to_sym if m[0] == "_"
  array = []
  no_key = true
  self.map do |hash|
    if hash.is_a?(Hash)
      array << hash[m]
      no_key = false
    else
      array << nil
    end
  end
  if no_key
    super(m, *arguments, &block)
  else
    array
  end
end
nice_filter(keys) click to toggle source

Filter the array of hashes and returns only the specified keys More info: NiceHash.nice_filter

# File lib/nice/hash/add_to_ruby.rb, line 108
def nice_filter(keys)
  NiceHash.nice_filter(self, keys)
end