unknown wrote:
> Hi --
> 
> On Wed, 6 Dec 2006, Andreas Warberg wrote:
> 
>> How come the scope works in this way? I expected the visibility of
>> variables to flow down into the code tree (but not up, of course).
> 
> def starts a new local scope, so local variables defined outside it
> won't be visible.  I can't answer the question defensively -- that is,
> I can't say why it is in relation to Java, since I have no reason to
> think it was designed in relation to Java :-)
> 
> Instance variables (@this) are always associated with, and owned by,
> whatever object is in the role of "self", the default object.  Inside
> a method definition, self is (or is going to be, when the method
> eventually gets called) the object running the method.
> 
> Global variables ($this) are just global variables, and have the usual
> properties of such variables: they walk through walls, so to speak,
> when it comes to scope, and generally discourage nice encapsulation of
> code and interaction of objects.
> 
> 
> David

Thanks for your replies.

It would seems this is just something I have "gotten used to" with java. 
Looking over some of the things I did in the past (in learning ruby) it 
is clear that constants (variables with capital starting letter), which 
do flow past def statements could have removed a great part of my $'s. 
To illustrate the difference between local and constant variables:

x=1
def print_local
   puts x
end

(fails)

X=1
def print_constant
   puts X
end

(prints value of capital X)

Best regards
Andreas

-- 
Posted via http://www.ruby-forum.com/.