Class: Hash

Inherits:
Object show all
Defined in:
lib/build/jake.rb,
lib/framework/builtinME.rb,
lib/extensions/mspec/mspec/pp.rb,
lib/framework/indifferent_access.rb,
lib/framework/rholang/localization_simplified.rb

Instance Method Summary collapse

Instance Method Details

#eachObject Also known as: each_pair



343
344
345
346
# File 'lib/framework/builtinME.rb', line 343

def each
    ks = keys
    ks.each {|k| yield([k, self[k]])}
end

#each_keyObject



348
349
350
351
# File 'lib/framework/builtinME.rb', line 348

def each_key
    ks = keys
    ks.each {|k| yield(k)}
end

#each_valueObject



353
354
355
356
# File 'lib/framework/builtinME.rb', line 353

def each_value
    vs = values
    vs.each {|k| yield(k)}
end

#empty?Boolean

Returns:

  • (Boolean)


358
359
360
# File 'lib/framework/builtinME.rb', line 358

def empty?
    length == 0
end

#fetch_r(key) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/build/jake.rb', line 41

def fetch_r(key)
  if self.has_key?(key) and not self[key].is_a?(Hash)
    return self[key]
  else
    self.each do |val|
	value = false
	if val.is_a?(Array)
        val.each do |x|
          value = x.fetch_r(key) if x.is_a?(Hash)
	    return value if value
	  end
end
      value = val.fetch_r(key) if val.is_a?(Hash)
	return value if value
    end
  end
  return false
end

#index(value) ⇒ Object



409
410
411
412
# File 'lib/framework/builtinME.rb', line 409

def index value
    each {|k, v| return k if value == v }
    return nil
end

#inspectObject Also known as: to_s



375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/framework/builtinME.rb', line 375

def inspect
    r = '{'
    is_first = true
    each_pair do |k, v|
        if !is_first
            r << ", "
        end
        is_first = false
        r << k.inspect
        r << '=>'
        r << v.inspect
    end
    r << '}'
end

#invertObject



392
393
394
395
396
# File 'lib/framework/builtinME.rb', line 392

def invert
    h = {}
    each {|k, v| h[v] = k}
    h
end

#merge(other) ⇒ Object



405
406
407
# File 'lib/framework/builtinME.rb', line 405

def merge other
    clone.merge!(other)
end

#pretty_print(q) ⇒ Object



719
720
721
# File 'lib/extensions/mspec/mspec/pp.rb', line 719

def pretty_print(q)
  q.pp_hash self
end

#pretty_print_cycle(q) ⇒ Object



723
724
725
# File 'lib/extensions/mspec/mspec/pp.rb', line 723

def pretty_print_cycle(q)
  q.text(empty? ? '{}' : '{...}')
end

#replace(other) ⇒ Object



414
415
416
417
# File 'lib/framework/builtinME.rb', line 414

def replace(other)
    clear
    update other
end

#reverse_merge(other_hash) ⇒ Object



92
93
94
# File 'lib/framework/rholang/localization_simplified.rb', line 92

def reverse_merge(other_hash)
    other_hash.merge(self)
end

#reverse_merge!(other_hash) ⇒ Object



96
97
98
# File 'lib/framework/rholang/localization_simplified.rb', line 96

def reverse_merge!(other_hash)
    replace(reverse_merge(other_hash))
end

#stringify_keysObject



100
101
102
103
104
105
106
107
108
# File 'lib/framework/rholang/localization_simplified.rb', line 100

def stringify_keys
  options = {}
  
  each_pair do |key, value|
    options[key.to_s] = value
  end
  
  options
end

#symbolize_keysObject



132
133
134
135
136
137
138
# File 'lib/framework/indifferent_access.rb', line 132

def symbolize_keys
  hash = {}
  self.each do |key, value|
    hash[(key.to_sym rescue key) || key] = value
  end
  hash
end

#to_aObject



364
365
366
367
368
369
370
371
372
373
# File 'lib/framework/builtinME.rb', line 364

def to_a
    res = []
    each_pair do |k, v|
      item = []
      item << k
      item << v
      res << item
    end
    res
end

#update(other) ⇒ Object Also known as: merge!



398
399
400
401
# File 'lib/framework/builtinME.rb', line 398

def update other
    other.each {|k, v| self[k] = v}
    self
end

#with_indifferent_accessObject



127
128
129
130
131
# File 'lib/framework/indifferent_access.rb', line 127

def with_indifferent_access
  hash = HashWithIndifferentAccess.new(self)
  hash.default = self.default
  hash
end