Sha256: 53baf69779bdb4e81c7fe7dfd8926616860f680be8629656f290d678e2d6bb7b

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

module Kernel

  # Replace state of object with the state of another object of the
  # same class (or superclass).
  #
  #     class ReplaceExample
  #       attr_reader :a, :b
  #       def initialize(a,b)
  #         @a, @b = a, b
  #       end
  #     end
  #
  #     obj1 = ReplaceExample.new(1,2)
  #     obj1.a  #=> 1
  #     obj1.b  #=> 2
  #
  #     obj2 = ReplaceExample.new(3,4)
  #     obj2.a  #=> 3
  #     obj2.b  #=> 4
  #
  #     obj1.instance_replace(obj2)
  #     obj1.a  #=> 3
  #     obj1.b  #=> 4
  #
  # This is very similar to `instance.assign`, but it is limited
  # by the class of object, in the same manner as Array#replace.
  #
  # NOTE: Conceptually a better alternative is `instance.replace` provided
  #       by the `instance` gem. However, practically this method is probably 
  #       the better choice until such time that Ruby support anonymous delegators.
  #
  def instance_replace(source)
    raise ArgumentError, "not a #{self.class} -- #{source}" unless source.is_a?(self.class)
    instance_variables.each do |iv|
      instance_variable_set(iv, source.instance_variable_get(iv))
    end
  end

end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/kernel/instance_replace.rb
facets-3.1.0 lib/core/facets/kernel/instance_replace.rb
facets-3.0.0 lib/core/facets/kernel/instance_replace.rb