Module: Corefines::Object::InstanceValues

Defined in:
lib/corefines/object.rb

Instance Method Summary collapse

Instance Method Details

#instance_valuesHash

Returns a hash with symbol keys that maps instance variable names without “@” to their corresponding values.

Examples:

class C
  def initialize(x, y)
    @x, @y = x, y
  end
end

C.new(0, 1).instance_values
=> {x: 0, y: 1}

Returns:

  • (Hash)

    a hash with symbol keys that maps instance variable names without “@” to their corresponding values.


189
190
191
192
193
194
195
196
197
198
# File 'lib/corefines/object.rb', line 189

module InstanceValues
  refine ::Object do
    def instance_values
      ary = instance_variables.map do |name|
        [ name[1..-1].to_sym, instance_variable_get(name) ]
      end
      ::Hash[ary]
    end
  end
end