class String
Public Instance Methods
==(par)
click to toggle source
When comparing an string and an integer, float or nil, it will be automatically converted to string:
"300" == 300 #will return true 200.1=="200.1" #will return true ""==nil #will return true
Calls superclass method
# File lib/nice/hash/add_to_ruby.rb, line 8 def ==(par) if par.is_a?(Integer) || par.nil? || par.is_a?(Float) super(par.to_s) else super(par) end end
json(*keys)
click to toggle source
In case the string is a json it will return the keys specified. the keys need to be provided as symbols. In case the string is not a json then it will notify the error and return empty Hash
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 returned 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: the json string as a ruby structure. if no json string or wrong json string, an empty hash. if one key supplied and doesn't exist on the json string then an empty hash
# File lib/nice/hash/add_to_ruby.rb, line 29 def json(*keys) require "json" result = {} begin feed_symbols = JSON.parse(self, symbolize_names: true) if !keys.empty? result_tmp = if keys[0].is_a?(Symbol) NiceHash.get_values(feed_symbols, keys) else {} end if result_tmp.size == 1 result = if result_tmp.values.is_a?(Array) && (result_tmp.values.size == 1) if result_tmp.values[0].nil? {} else result_tmp.values[0] end else result_tmp.values end else result_tmp.each do |key, value| result[key] = if (value.is_a?(Array) || value.is_a?(Hash)) && (value.size == 1) value[0] else value end end end else result = feed_symbols end rescue StandardError => stack puts "#{stack.backtrace[-1]} #{stack.to_s}" puts stack.backtrace.join("\n\t") if $DEBUG end result end