[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Storage binding

Can Ceran

12/13/2006 1:29:00 AM

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?

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

4 Answers

Joel VanderWerf

12/13/2006 4:14:00 AM

0

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.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

benjohn

12/13/2006 8:45:00 AM

0

> 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

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


Robert Klemme

12/13/2006 9:57:00 AM

0

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

Joel VanderWerf

12/13/2006 11:34:00 PM

0

benjohn@fysh.org wrote:
>> 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
>
> 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

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407