class Hash

Public Instance Methods

bury(where, value) click to toggle source

Stores a value on the location indicated input:

where: (Array)
value

examples:

my_hash.bury([:bip, :doom], "doom") # hash of hash
my_hash.bury([:original, 1, :doom],"the value to set") #hash of array of hash
# File lib/nice/hash/add_to_ruby.rb, line 185
def bury(where, value)
  me = self
  where[0..-2].each do |key|
    me = me[key]
  end
  key = where[-1]
  key = [key] unless where[-1].is_a?(Array) # for the case same value for different keys, for example pwd1, pwd2, pwd3
  key.each do |k|
    me[k] = value
  end
end
deep_copy() click to toggle source

returns a clean copy of the hash

# File lib/nice/hash/add_to_ruby.rb, line 200
def deep_copy
  Marshal.load(Marshal.dump(self))
end
gen(select_hash_key = nil, expected_errors: [], **synonyms)
Alias for: generate
generate(select_hash_key = nil, expected_errors: [], **synonyms) click to toggle source

It will generate a new hash with the values generated from the string patterns and select fields specified. In case supplied select_hash_key and a subhash specified on a value it will be selected only the value of the key specified on select_hash_key If expected_errors specified the values will be generated with the specified errors. More info: NiceHash.generate alias: gen

# File lib/nice/hash/add_to_ruby.rb, line 220
def generate(select_hash_key = nil, expected_errors: [], **synonyms)
  NiceHash.generate(self, select_hash_key, expected_errors: expected_errors, **synonyms)
end
Also aliased as: gen
get_values(*keys) click to toggle source

Get values of the keys supplied from the Hash structure. More info: NiceHash.get_values

# File lib/nice/hash/add_to_ruby.rb, line 261
def get_values(*keys)
  NiceHash.get_values(self, keys.flatten)
end
has_rkey?(search) click to toggle source

Search if the hash contains the supplied key search can be a string, symbol or regexp. In case of string or symbol it will return true even if only part of the key fits the 'search'

# File lib/nice/hash/add_to_ruby.rb, line 278
def has_rkey?(search)
  search = Regexp.new(search.to_s) unless search.is_a?(Regexp)
  !!keys.detect{ |key| key =~ search }
end
method_missing(m, *arguments, &block) click to toggle source

Returns the value of the key specified in case doesn't exist a Hash method with the same name The keys can be accessed also adding underscore to avoid problems with existent methods Also set values in case = supplied examples:

my_hash.address.correct
my_hash._address._correct
my_hash.city
my_hash._city
my_hash.city="Paris"
my_hash.products[1].price.wrong="AAAAA"
# File lib/nice/hash/add_to_ruby.rb, line 159
def method_missing(m, *arguments, &block)
  m = m[1..-1].to_sym if m[0] == "_"
  if key?(m)
    self[m]
  elsif key?(m.to_s)
    self[m.to_s]
  elsif m.to_s[-1] == "="
    if key?(m.to_s.chop)
      self[m.to_s.chop] = arguments[0]
    else
      self[m.to_s.chop.to_sym] = arguments[0]
    end
  else
    nil
  end
end
nice_filter(keys) click to toggle source

Filter the hash and returns only the specified keys More info: NiceHash.nice_filter

# File lib/nice/hash/add_to_ruby.rb, line 287
def nice_filter(keys)
  NiceHash.nice_filter(self, keys)
end
pattern_fields(*select_hash_key) click to toggle source

It will return an array of the keys where we are using string patterns. More info: NiceHash.pattern_fields

# File lib/nice/hash/add_to_ruby.rb, line 245
def pattern_fields(*select_hash_key)
  NiceHash.pattern_fields(self, *select_hash_key)
end
Also aliased as: patterns
patterns(*select_hash_key)
Alias for: pattern_fields
select_fields(*select_hash_key) click to toggle source

It will return an array of the keys where we are using select values of the kind: “value1|value2|value3”. More info: NiceHash.select_fields

# File lib/nice/hash/add_to_ruby.rb, line 253
def select_fields(*select_hash_key)
  NiceHash.select_fields(self, *select_hash_key)
end
select_key(select_hash_key) click to toggle source

It will filter the hash by the key specified on select_hash_key. In case a subhash specified on a value it will be selected only the value of the key specified on select_hash_key More info: NiceHash.select_key

# File lib/nice/hash/add_to_ruby.rb, line 209
def select_key(select_hash_key)
  NiceHash.select_key(self, select_hash_key)
end
set_values(hash_values) click to toggle source

It will search for the keys supplied and it will set the value specified More info: NiceHash.set_values

# File lib/nice/hash/add_to_ruby.rb, line 269
def set_values(hash_values)
  NiceHash.set_values(self, hash_values)
end
val(select_hash_key = nil, values_hash_to_validate)
Alias for: validate
validate(select_hash_key = nil, values_hash_to_validate) click to toggle source

Validates a given values_hash_to_validate with string patterns and select fields More info: NiceHash.validate alias: val

# File lib/nice/hash/add_to_ruby.rb, line 229
def validate(select_hash_key = nil, values_hash_to_validate)
  NiceHash.validate([self, select_hash_key], values_hash_to_validate, only_patterns: false)
end
Also aliased as: val
validate_patterns(select_hash_key = nil, values_hash_to_validate) click to toggle source

Validates a given values_hash_to_validate with string patterns More info: NiceHash.validate

# File lib/nice/hash/add_to_ruby.rb, line 237
def validate_patterns(select_hash_key = nil, values_hash_to_validate)
  NiceHash.validate([self, select_hash_key], values_hash_to_validate, only_patterns: true)
end