Class: Hash

Inherits:
Object show all
Defined in:
lib/cosmos/core_ext/hash.rb,
lib/cosmos/io/json_rpc.rb,
lib/cosmos/config/meta_config_parser.rb

Overview

COSMOS specific additions to the Ruby Hash class

Instance Method Summary collapse

Instance Method Details

#as_json(options = nil) ⇒ Object

:nodoc:



108
109
110
111
112
# File 'lib/cosmos/io/json_rpc.rb', line 108

def as_json(options = nil) #:nodoc:
  pairs = []
  self.each { |k, v| pairs << k.to_s; pairs << v.as_json(options) }
  Hash[*pairs]
end

#inspect(max_elements = 10) ⇒ String

Returns String representation of the hash.

Parameters:

  • max_elements (Integer) (defaults to: 10)

    The maximum number of elements in the Hash to print out before simply displaying the Hash class and object id

Returns:

  • (String)

    String representation of the hash



30
31
32
33
34
35
36
# File 'lib/cosmos/core_ext/hash.rb', line 30

def inspect(max_elements = 10)
  if self.length <= max_elements
    old_inspect()
  else
    '#<' + self.class.to_s + ':' + self.object_id.to_s + '>'
  end
end

#old_inspectObject

Redefine inspect to only print for small numbers of items. Prevents exceptions taking forever to be raised with large objects. See blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/105145



25
# File 'lib/cosmos/core_ext/hash.rb', line 25

alias old_inspect inspect

#to_meta_config_yaml(indentation = 0) ⇒ Object



32
33
34
# File 'lib/cosmos/config/meta_config_parser.rb', line 32

def to_meta_config_yaml(indentation = 0)
  Psych.dump(self).split("\n")[1..-1].join("\n#{' ' * indentation}")
end