Class: String
Instance Method Summary collapse
-
#==(par) ⇒ Object
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.
-
#json(*keys) ⇒ Object
In case the string is a json it will return the keys specified.
Instance Method Details
#==(par) ⇒ Object
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
10 11 12 13 14 15 16 |
# File 'lib/nice/hash/add_to_ruby.rb', line 10 def ==(par) if par.is_a?(Integer) || par.nil? || par.is_a?(Float) super(par.to_s) else super(par) end end |
#json(*keys) ⇒ Object
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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/nice/hash/add_to_ruby.rb', line 31 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 |