Storage binding

Hi,
I have also some another thing in Ruby that I am not sure about.
What kind of storage binding is used in Ruby, as far as I understand
there are no static variables (i’m not sure again), then Ruby uses both
stack-dynamic and heap-dynamic variables or just stack-dynamic?

Can Ceran wrote:

Hi,
I have also some another thing in Ruby that I am not sure about.
What kind of storage binding is used in Ruby, as far as I understand
there are no static variables (i’m not sure again), then Ruby uses both
stack-dynamic and heap-dynamic variables or just stack-dynamic?

You can determine storage by the notation:

stack

x = 1
… {|x|…}
def meth x

heap

@x = 1
@@x = 1
$x = 1

All ruby vars are statically (lexically) scoped.

stack

x = 1
… {|x|…}
def meth x

heap

@x = 1
@@x = 1
$x = 1

I’m pretty sure you’re being misleading there. You can determine the
scope of a variable by it’s notation / prefix.

Function’s scope: x=1
Object’s scope: @x=1
Class’s scope: @@x=1
Global scope: $x=1

Variables themselves are all references to objects. When you do:
a = b
or
@a = b
etc

You are really saying:
“make the variable ‘a’ reference what ever the variable ‘b’ is
referencing.”

You are not actually changing any of the underlying objects (this is
completely different from c++, for example).

As far as I know, the storage of the actual objects is heap based, but
really, that’s an implementation issue. You don’t care where it is
(probably). You’re not exposed to this concept by the semantics in the
Ruby programming model.

Cheers,
Benjohn

[email protected] wrote:


I’m pretty sure you’re being misleading there. You can determine the
scope of a variable by it’s notation / prefix.

Oops, right. That was very misleading. I was talking about where the
variable is stored (which Robert explained better). The question was
probably about where the object is stored, and that’s always on the
heap.

The scope of a variable is not, strictly speaking, determined by prefix,
however:

def foo
x = 1
3.times { x||=1; x+=1 }
p x # ==> 4
end

def foo
3.times { x||=1; x+=1 }
p x # ==> NameError
end

On 13.12.2006 02:29, Can Ceran wrote:

What kind of storage binding is used in Ruby, as far as I understand
there are no static variables (i’m not sure again), then Ruby uses both
stack-dynamic and heap-dynamic variables or just stack-dynamic?

Objects are always on the heap (there are special cases like Fixnums
but it’s simplest to think of all objects on the heap). Method
parameters and local variables are on the stack and methods are called
by value - although the value is an object reference (like a C function
having only pointer arguments). Consequently instance variables and
class variables are located on the heap (as part of an object).

Kind regards

robert