Local variables & blocks

(guess no one read my other message on this)

I did. I'm just not really qualified to comment. But I will anyway :slight_smile:

It always seemed strange to me that 'local variables' were not really part
of an object, and therefore seemed to break the OO model. I think what you
are proposing is to turn them into a special sort of instance variable: so

    x = 1

is a bit like

    @x = 1

except that the second case persists with the object for its entire
lifetime, whereas the first case persists only until the end of, erm, I'm
not quite sure - the current lexical scope? Perhaps then a new lexical scope
create a temporary subclass where these instance variables are deposited?
All method calls (including instances of local variables) will then follow
the normal chain of calling back up to the parent.

I'm not sure how this differs from the current situation though, if you
replace "temporary subclass" with "stack frame".

I suppose it might make local variables more widely available - for example:

class Foo
  def bar
    puts i
  end
  def baz
    i = 10
    bar
  end
end

Foo.new.baz # doesn't work now

would allow the value of 'i' to be seen in 'bar' when called from 'baz'. If
that were desirable though, I'd have thought the current implementation
could allow it (e.g. by searching through all the stack frames, not just the
current one)

Regards,

Brian.

(guess no one read my other message on this)

I did. Iā€™m just not really qualified to comment. But I will anyway :slight_smile:

sure you are. i really just needed some feed back to stir my own brain cells.
thanks.

It always seemed strange to me that ā€˜local variablesā€™ were not really part
of an object, and therefore seemed to break the OO model. I think what you
are proposing is to turn them into a special sort of instance variable: so

x = 1

is a bit like

@x = 1

indeed, thatā€™s exactly what bugs me too.

except that the second case persists with the object for its entire
lifetime, whereas the first case persists only until the end of, erm, Iā€™m
not quite sure - the current lexical scope?

yes the scope

Perhaps then a new lexical
scope create a temporary subclass where these instance variables are
deposited? All method calls (including instances of local variables) will
then follow the normal chain of calling back up to the parent.

hmmmā€¦now that gives me a notionā€¦

Iā€™m not sure how this differs from the current situation though, if you
replace ā€œtemporary subclassā€ with ā€œstack frameā€.

I suppose it might make local variables more widely available - for
example:

class Foo
def bar
puts i
end
def baz
i = 10
bar
end
end

Foo.new.baz # doesnā€™t work now

would allow the value of ā€˜iā€™ to be seen in ā€˜barā€™ when called from ā€˜bazā€™. If
that were desirable though, Iā€™d have thought the current implementation
could allow it (e.g. by searching through all the stack frames, not just
the current one)

essentially if it were seen by bar defined by baz then it would be pretty much
the same as @i. hey, perhaps we donā€™t need no stinkā€™en locals :wink:

i realize now that self canā€™t really define the scope. b/c then any definition
would actually bind self to it . i.e.

class Foo
def bar
self #ā€“> normally instance of Foo, but scope is Foo#bar
end
end

perhaps having an attribute of the object actually called scope would be of
interest?

class Foo
def bar
self #ā€“> instance of Foo
scope #ā€“> instance of Fooā€™s bar method
end
end

thus local variables such as:

x = 1

are in effect:

scope.x = 1

Thanks,
tom sawyer, aka transami
transami@transami.net

Ā·Ā·Ā·

On Saturday 01 February 2003 05:59 am, Brian Candler wrote:

Then again, instead of calling it ā€˜scopeā€™ you can call it local

local.x = 1

and then, we make some syntactic sugar:
ā€˜local.ā€™ <=> ā€˜&!@ā€™

and we get to
&!@x = 1

OH MY GOD! :wink:

This is anyway cleaner than self[i], cause the closure only wraps one
stack frame. However it is still very powerful as it captures all
locals.

def foo
a = 1
b = 2
scope # gets all the locals
end

foo.a # 1

It would be possible to implement this in pure Ruby if we could
define a macro that used #binding .

If you go the way of adding a prefix for locals you cannot possibly beat
something like ā€˜&localā€™; moreover that notation doesnā€™t create
ā€˜super-closuresā€™. The point is it seems we have something against
adding line noise :slight_smile:

Ummm Iā€™m thinking of the weirdness of having instance method objectsā€¦

class Bar
def foo
end
class << foo
def bar

Now, Iā€™m an instance method singleton method. Uh?

end
iamAlocalOfFoo = true
end
end

If everything is going to be an object, Ruby is going to be Smalltalk :wink:

Ā·Ā·Ā·

On Sat, Feb 01, 2003 at 10:57:42PM +0900, Tom Sawyer wrote:

i realize now that self canā€™t really define the scope. b/c then any definition
would actually bind self to it . i.e.

class Foo
def bar
self #ā€“> normally instance of Foo, but scope is Foo#bar
end
end

perhaps having an attribute of the object actually called scope would be of
interest?

class Foo
def bar
self #ā€“> instance of Foo
scope #ā€“> instance of Fooā€™s bar method
end
end

thus local variables such as:

x = 1

are in effect:

scope.x = 1

ā€“
_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ā€™ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

  • dpkg ponders: ā€˜C++ā€™ should have been called ā€˜Dā€™
    ā€“ #Debian