Module: JSI::Hashlike

Included in:
BaseHash, JSON::HashNode
Defined in:
lib/jsi/typelike_modules.rb

Overview

a module of methods for objects which behave like Hash but are not Hash.

this module is intended to be internal to JSI. no guarantees or API promises are made for non-JSI classes including this module.

Constant Summary collapse

SAFE_KEY_ONLY_METHODS =

methods which do not need to access the value.

%w(each_key empty? has_key? include? key? keys length member? size)
SAFE_KEY_VALUE_METHODS =
%w(< <= > >= any? assoc compact dig each_pair each_value fetch fetch_values has_value? invert key merge rassoc reject select to_h to_proc transform_values value? values values_at)
DESTRUCTIVE_METHODS =
%w(clear delete delete_if keep_if reject! replace select! shift)
SAFE_METHODS =
SAFE_KEY_ONLY_METHODS | SAFE_KEY_VALUE_METHODS

Instance Method Summary collapse

Instance Method Details

#inspectString

Returns basically the same #inspect as Hash, but has the class name and, if responsive, self's #object_group_text.

Returns:

  • (String)

    basically the same #inspect as Hash, but has the class name and, if responsive, self's #object_group_text



104
105
106
107
# File 'lib/jsi/typelike_modules.rb', line 104

def inspect
  object_group_text = respond_to?(:object_group_text) ? ' ' + self.object_group_text : ''
  "\#{<#{self.class}#{object_group_text}>#{empty? ? '' : ' '}#{self.map { |k, v| "#{k.inspect} => #{v.inspect}" }.join(', ')}}"
end

#pretty_print(q) ⇒ void

This method returns an undefined value.

pretty-prints a representation this node to the given printer



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/jsi/typelike_modules.rb', line 116

def pretty_print(q)
  q.instance_exec(self) do |obj|
    object_group_text = obj.respond_to?(:object_group_text) ? ' ' + obj.object_group_text : ''
    text "\#{<#{obj.class}#{object_group_text}>"
    group_sub {
      nest(2) {
        breakable(obj.any? { true } ? ' ' : '')
        seplist(obj, nil, :each_pair) { |k, v|
          group {
            pp k
            text ' => '
            pp v
          }
        }
      }
    }
    breakable ''
    text '}'
  end
end

#to_sString

Returns see #inspect.

Returns:

  • (String)

    see #inspect



110
111
112
# File 'lib/jsi/typelike_modules.rb', line 110

def to_s
  inspect
end