inheriting and copying class variables

stevenspiel

This is a contrived example to emulate what is going on in another project. I'm sure I'm just misunderstanding some basic function of classes, but I don't know where to look to find the answer.

If I set an instance variable @empty = [] in the parent class, then I want to use the original instance variable, as well as a variation of the instance variable in a child class, I'm getting an issue where the original instance variable is no longer accessible in its original form.

class Parent
  def initialize
    @empty = []
  end

  def fill
    @empty << "foo" #interestingly, changing the << to = fixes it, but that's not a possibility on my project
  end
end

class Child < Parent
  def initialize
    super
    @should_still_be_empty = @empty #trying to "make a copy" of the original
  end

  def show_original_variable
    @should_still_be_empty
  end

end

child = Child.new
child.fill #=> ["foo"], which is what it should return
child.show_original_variable #=> ["foo"], I want it to return []


UPDATE:

After playing around a little more, I noticed that this happens within the same classes, as well. Why?

class Child 
  def initialize
    @original_empty = []
    @copy_of_empty = @original_empty
  end

  def fill
    @copy_of_empty << "foo"
  end

  def show_empty
    @original_original_variable
  end

end

child = Child.new
child.fill #=> ["foo"]
child.show_original_variable #=> ["foo"], but I want it to be empty
FMc

You question has nothing in particular to do with classes or inheritance. Rather, it deals with more basic issues related to assignment and mutation.

xs = [11,22,33]
ys = xs           # Both variables hold a reference to the same data.

zs = xs.dup       # But zs has a copy (at least a shallow copy).

xs << 44          # If we *mutate* xs, ys changes also.

p xs              # [11, 22, 33, 44]
p ys              # [11, 22, 33, 44]
p zs              # [11, 22, 33]

xs = [4,5,6]      # If we *assign* to xs, ys won't be affected.

p xs              # [4,5,6]
p ys              # [11, 22, 33, 44]

The same behaviors would be observed even if the xs array started out empty (as in your code), but it's a little easier to illustrate with data values present.

If you want @should_still_be_empty to have an independent copy of the underlying data values, you need to do something along these lines:

@should_still_be_empty = @empty.dup

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

inheriting constructors from base class?

From Dev

Class is not inheriting DB correctly

From Dev

Purpose of property copying to inheriting type

From Dev

Ruby inheriting a module class not working

From Dev

python - inheriting parent class

From Dev

Inheriting from a static class

From Dev

Abstract class inheriting from RecursiveTask

From Dev

Backbone class model not inheriting defaults

From Dev

Ignore [ScriptIgnore] Attribute in Inheriting Class

From Dev

Python: new class, inheriting and overriding

From Dev

Terminology for class inheriting from dict

From Dev

Jenkins - Inheriting environment variables on Ubuntu

From Dev

Inheriting a patched class

From Dev

Inheriting module/class methods

From Dev

Inheriting initializer from generic class

From Dev

Inheriting class with primary constructor

From Dev

Inheriting Abstract Class Attributes that are final

From Dev

inheriting and copying class variables

From Dev

Inheriting from an existing css class

From Dev

Inheriting from a child class of Window

From Dev

Xamarin : Inheriting from AlertDialog class

From Dev

Inheriting class with required constructor in generics

From Dev

Python: new class, inheriting and overriding

From Dev

Class name not found when inheriting

From Dev

Web API - Class variables retained across requests in class inheriting AuthorizeAttribute

From Dev

Inheriting from inner class

From Dev

CSS not inheriting parent class properties

From Dev

Are there pros to inheriting a class template?

From Dev

Inheriting into Base Class Namespace