Sha256: ff28d9d209578fc46b8f334fa3a9f004f8622b4b251fe5b84131d073f25c4a97

Contents?: true

Size: 855 Bytes

Versions: 3

Compression:

Stored size: 855 Bytes

Contents

# frozen_string_literal: true

class ObjectShadow
  module InstanceVariables
    # Returns the instance variable given
    # Please note: Does not expect @ prefix
    def [](ivar_name)
      object.instance_variable_get(:"@#{ivar_name}")
    end

    # Sets the instance variable given
    # Please note: Does not expect @ prefix
    def []=(ivar_name, value)
      object.instance_variable_set(:"@#{ivar_name}", value)
    end

    def remove(ivar_name)
      object.remove_instance_variable(:"@#{ivar_name}")
    end

    def variable?(ivar_name)
      object.instance_variable_defined?(:"@#{ivar_name}")
    end

    def variables
      object.instance_variables.map{ |ivar| ivar[1..-1].to_sym }
    end

    def to_h
      variables.map{ |ivar| [ivar, self[ivar]] }.to_h
    end

    def to_a
      variables.map{ |ivar| self[ivar] }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
object_shadow-1.1.1 lib/object_shadow/instance_variables.rb
object_shadow-1.1.0 lib/object_shadow/instance_variables.rb
object_shadow-1.0.0 lib/object_shadow/instance_variables.rb