Class: Array

Inherits:
Object show all
Defined in:
lib/nice/hash/add_to_ruby.rb,
lib/nice/hash/add_to_ruby.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *arguments, &block) ⇒ Object

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 ['Peter', city: 'Madrid', 'Lola', city: 'NYC'] : my_array.city my_array._name



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# 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

Instance Method Details

#bury(where, value) ⇒ Object

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



80
81
82
83
84
85
86
# 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) ⇒ Object

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



98
99
100
101
# File 'lib/nice/hash/add_to_ruby.rb', line 98

def json(*keys)
  json_string = "[#{join(",")}]"
  json_string.json(*keys)
end

#nice_filter(keys) ⇒ Object

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



108
109
110
# File 'lib/nice/hash/add_to_ruby.rb', line 108

def nice_filter(keys)
  NiceHash.nice_filter(self, keys)
end