[ruby-talk:02512] Refering to another instance with a method

From: "David Douthitt" <DDouthitt@...>
Date: 2000-04-19 17:31:18 UTC
List: ruby-talk #2512
I have a situation where I want to copy values from one instance to another with a particular method.  Put another way, I want to set values in an instance to default values after the instance has been created.  The default values may or may not be known at instance creation time.

Seems like there are two ways to do this:

1. Create an "instance" with default values, then copy the values from this instance to another instance.  How do I do this?

2. Create class variables and fill them with default values when appropriate, then copy these default values into an instance.  How do I do this?  I thought I heard something about using constants, but I thought I did something in one of my programs using either local variables (no special markings) or instance variables (with "@") outside any methods.  I'm still looking for it.

As a corollary to #2, tell me how these work (or don't):

class MyClass
   myvar1 = "a"
   @myvar2 = "b"

   def method
      p myvar1, @myvar2
   end
end

It seems as if @myvar2 and its ilk are the way to go, but when I try to set them in a method or something everything gets all fouled up.

This last question sounds like a good scope question...............


In This Thread

Prev Next