Class: String

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

Instance Method Summary collapse

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



9
10
11
12
13
14
15
# File 'lib/nice/hash/add_to_ruby.rb', line 9

def ==(par)
  if par.kind_of?(Integer) or par.nil? or par.kind_of?(Float) then
    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 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 op arrays if no keys given, an empty hash



27
28
29
30
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
# File 'lib/nice/hash/add_to_ruby.rb', line 27

def json(*keys)
  require 'json'
  feed_symbols = JSON.parse(self, symbolize_names: true)
  result = {}
  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)
                 result_tmp.values[0]
               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
  result
end