Hi --

On Mon, 30 Nov 2009, Ralph Shnelvar wrote:

> DAB> This is the so-called "class instance variable", which is really just
> DAB> "an instance variable that happens to belong to a Class object". It's
> DAB> common to use the phrase "class instance variable", but it's just a
> DAB> way of clarifying what you mean. There's no separate "class instance
> DAB> variable" language-level construct.
>
> So ... let's see if I get this.
>
> class X
>  # "equivalent" to C++'s
>  # class X {
>  #   protected:
>  #   static sometype at_at_x = 3;
>  # };
>  @@x = 3
>
>  # "equivalent" to C++'s
>  # class X {
>  #   private:
>  #   sometype at_x;
>  #   X::X() {at_x =3;}
>  # };
>  # at_x is not "inheritable" since it is private.
>  @x = 3

@x belongs to self. Inside a class definition, self is the class:

   class X
     p self     # Output: X
   end

Therefore, if you create an instance variable, it belongs to X (the
class object):

   class X
     @var = 1  # this @var belongs to the class object X (i.e., self)
   end

   p X.instance_variables   # Output: ["@var"]

The same thing happens with EVERY object in Ruby. If self is object x,
then @var is an instance variable of object x:

   class C
     def greet
       p self
       @var = 1
       p instance_variables
     end
   end

   C.new.greet

Output:

   #<C:0x484554>   (i.e., an instance of class C)
   ["@var"]        (the i.vars of that instance)

   str = "I'm a string"
   str.instance_eval do   # instance_eval sets "self" for the
     @var = 1             # duration of a code block.
     p self
     p self.instance_variables
   end

Output:

   "I'm a string"
   ["@var"]

I repeat: instance variables are always pegged to "self". They are not
usefully understood as private or public. You can say, informally,
that instance variables are "private", in the sense that they only
appear when self is the object that owns them. But it's kind of
pointless since there's no other state they can be in; there's no such
thing as a "public" instance variable.

>  # No equivalent notion in C++.  It's just a temporary variable
>  x = 3
> end
>
>
>
> - - - -
>
>
>
> It appears to me that "class" is a jumble of two kinds of very
> different semantics.
>
> There is the notion of a class as a template for instances of the
> class.
>
> Then there is a very different notion of a sorta static class that
> has it's own variables that do not propagate to instances; and may
> (@@x) or may not (@x) propagate and be accessile to static subclasses inherited from
> the static superclass.

I have to admit that in nine years of using, teaching, and writing
books about Ruby, I don't think I've ever used the word "static" to
describe anything in Ruby. Ruby and "static" don't mix :-)

> A lot to wrap one's head around.  I assume that there are good design
> reasons to jumble these two concepts together in the semantics of
> "class".

I'm a little baffled by why you're trying to understand Ruby by
transliterating concepts and even syntax from C++, and then concluding
that Ruby is a "jumble" because there's no one-to-one correspondence.
I'd really, really urge you to forget about C++ when learning Ruby.
(Easy for me because I don't know C++ in the first place :-)

In Ruby terms: a class is an object. It has a few special behaviors
that set it apart from other objects, mainly:

   1. it can spawn other objects
   2. it has a keyword dedicated to switching into its "self" scope:
      class

The Class class is a subclass of the Module class. Modules and classes
are storage units for methods (which objects can cause to be executed,
in response to the appropriate methods) and constants.

I would suggest forgetting C++, forgetting class variables, and
playing around with class definitions, the concept of "self", and
instance variables. Try to understand it in Ruby terms. It's actually
much more straightforward that way.


David

-- 
THE COMPLEAT RUBYIST, Ruby training with Black/Brown/McAnally!
January 22-23, Tampa, Florida
Info and registration at http://www.thecompleatrubyist.com
--------------------------------------
My new job: http://tinyurl.com/yfpn9hz