# Storage binding

**URL:** <https://www.ruby-forum.com/t/storage-binding/79192>\
**Category:** Ruby\
**Created:** [December 13, 2006, 2:29am UTC](https://www.ruby-forum.com/t/storage-binding/79192 "2006-12-13T02:29:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cannac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3be4f8/32.png) [@cannac](https://www.ruby-forum.com/u/cannac)\
**Post date:** [December 13, 2006, 2:29am UTC](https://www.ruby-forum.com/t/storage-binding/79192/1 "2006-12-13T02:29:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![cannac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3be4f8/32.png) [@cannac](https://www.ruby-forum.com/u/cannac)\
**Post date:** [December 13, 2006, 5:15am UTC](https://www.ruby-forum.com/t/storage-binding/79192/2 "2006-12-13T05:15:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cannac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3be4f8/32.png) [@cannac](https://www.ruby-forum.com/u/cannac)\
**Post date:** [December 13, 2006, 9:45am UTC](https://www.ruby-forum.com/t/storage-binding/79192/3 "2006-12-13T09:45:47Z")

</div>

> ## 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

---

<div class="post-metadata">

**Author:** ![cannac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3be4f8/32.png) [@cannac](https://www.ruby-forum.com/u/cannac)\
**Post date:** [December 14, 2006, 12:34am UTC](https://www.ruby-forum.com/t/storage-binding/79192/4 "2006-12-14T00:34:18Z")

</div>

removed\_email\_address@domain.invalid 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

---

<div class="post-metadata">

**Author:** ![cannac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3be4f8/32.png) [@cannac](https://www.ruby-forum.com/u/cannac)\
**Post date:** [December 13, 2006, 11:01am UTC](https://www.ruby-forum.com/t/storage-binding/79192/5 "2006-12-13T11:01:54Z")

</div>

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
```
