Storage binding

C

Can Ceran

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?
 
J

Joel VanderWerf

Can said:
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.
 
B

benjohn

Can said:
You can determine storage by the notation:

stack
-----
x = 1
... {|x|...}
def meth x

heap

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
 
R

Robert Klemme

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
 
J

Joel VanderWerf

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,220
Messages
2,571,128
Members
47,744
Latest member
FrederickM

Latest Threads

Top