Hi --

On Fri, 16 Aug 2002, MikkelFJ wrote:

>
> <dblack / candle.superlink.net> wrote in message
> news:Pine.LNX.4.44.0208151905140.9567-100000 / candle.superlink.net...
> > > Variables starting with @ are visible both inside and outside of
> functions.
> >
> > The only place that happens, I think, is at the "top level" of
> > execution.  Otherwise it's like this:
> <cut>
>
> Yes - when writing it I was seeking a conclusion - and I didn't really catch
> this difference and the subsequent section I wrote deals with relating
> static methods to class scope variables.
>
> It somehow seems like a bug because each file can be viewed as a class
> definition of the same root level class. This would be consistent except for
> the mentioned behaviour.

The availability of instance variables (@var) depends on what "self"
is at a given point -- that is, what instance's variables you're
trying to access.  At the top level, "self" is a bootstrapping default
Object (if that's a good way to put it), and the scope is "flattened",
so that there's no change inside a method definition:

  irb(main):002:0> p id
  537806448
  nil
  irb(main):003:0> def thing; p id; end
  nil
  irb(main):004:0> thing
  537806448
  nil

It doesn't behave like a class definition, because it isn't one :-)
There's nothing here to instantiate.

However, when you do define a class, the scope of the class and the
scope of its eventual objects is different:

  irb(main):005:0> class Thing; p id; def thing; p id; end; end
  537854410
  nil
  irb(main):006:0> Thing.new.thing
  537850560
  nil

> def Thing.static_show
> puts "Static class scope: @a is >#{@a}<"
> end
>
> The functions where @a at class scope is visible, are the static class
> methods.

That's true of all instances of objects: when the object is "self",
you can see its instance variables:

  irb(main):001:0> class Thing; def talk; @a = 10; puts @a; end; end
  nil
  irb(main):002:0> t = Thing.new
  #<Thing:0x4019b9d0>
  irb(main):003:0> t.talk
  10
  nil
  irb(main):004:0> def t.singleton_method; puts @a; end
  nil
  irb(main):005:0> t.singleton_method
  10

In this example, you can see @a from singleton_method because, during
the definition of this method, t is "self".  That's exactly what's
happening when you do:

  def Thing.whatever

During that definition, you can see Thing's instance variables, if
any.

>    class Thing
>      @a = 1
>      puts "Class scope: @a is >#{@a}<"
>
>      def show
>        puts "Instance scope: @a is >#{@a}<"
>      end
>    end
>
> def Thing.static_show
> puts "Instance scope: @a is >#{@a}<"

Was that supposed to be "Static scope"?  (It is in the output :-)

> end
>
> Thing.new.show
> Thing.static_show
>
> =>
>
> Class scope: @a is >1<
> Instance scope: @a is ><
> Static scope: @a is >1<

Basically any object can have instance variables; there's no
connection between one objects @a and another's -- even if the second
object is an instance of the first.

I'm just wondering... I know you mentioned in your last post that you
weren't using standard Ruby terminology.  Why not? :-)  Personally I
don't find the term "static" to be helpful; it seems to obscure the
underlying similarity of the mechanism, and how consistent they are
within and among objects (classes and others).  I suppose the same
could be said, in a sense, of the term "class method", since there's
no special term for the singleton methods of other specific types of
object.  Anyway, like I said, just wondering :-)


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav