# Immediate Values

**URL:** <https://www.ruby-forum.com/t/immediate-values/85885>\
**Category:** Ruby\
**Created:** [January 1, 2006, 8:08am UTC](https://www.ruby-forum.com/t/immediate-values/85885 "2006-01-01T08:08:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 1, 2006, 8:08am UTC](https://www.ruby-forum.com/t/immediate-values/85885/1 "2006-01-01T08:08:27Z")

</div>

On Dec 31, 2005, at 10:07 PM, Johannes F. wrote:

> Fixnums and a few other special types (symbols, true/false/nil,  
> floats?) are assigned as immediate values: Instead of storing a  
> pointer (or reference) to the value object, the variable stores the  
> value directly. So in  
> a=4  
> ‘a’ does not hold a reference, technically speaking, but rather the  
> immediate value 4.  
> This is an implementation issue, and is done for efficiency.

I understand that this is the standard explanation for this sort of  
thing but does anyone else feel that it doesn’t quite fit? If  
Fixnums can have instance variables then doesn’t it make more sense  
to think of a as containing a reference to the Fixnum object known  
as 4?

In what way does a contain the value 4?

$ irb  
irb(main):001:0\> a = 4  
=\> 4  
irb(main):002:0\> a.object\_id  
=\> 9  
irb(main):003:0\> 4.object\_id  
=\> 9

I’m pretty sure that it is the object ids that are stored in variables  
not the associated values. Ruby of course doesn’t actually allocate  
space  
for Fixnums but instead encodes the state of the referenced Fixnum in  
the  
object\_id itself. It can do this because Fixnum state is completely  
encoded by its identity (i.e., its object\_id).

I know I’m being pedantic (again) but I’d rather think of assignment  
as _always_ copying references. It is simpler that way. The  
fancy bit-twiddling/implementation issues really come into play  
when the variable is dereferenced not when assignment occurs. At least  
that is the way I’ve come to think about it. Does anyone else think  
about  
it that way?

Gary W.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 1, 2006, 8:54am UTC](https://www.ruby-forum.com/t/immediate-values/85885/2 "2006-01-01T08:54:00Z")

</div>

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> I know I’m being pedantic (again) but I’d rather think of assignment  
> as _always_ copying references. It is simpler that way. The  
> fancy bit-twiddling/implementation issues really come into play  
> when the variable is dereferenced not when assignment occurs. At least  
> that is the way I’ve come to think about it. Does anyone else think  
> about  
> it that way?
> 
> Gary W.

Some do (including me), but others don’t find it useful. e.g. See

[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/6371773a2dba0892/19eb6da6a6ae89fd?q=itsme213+encoding&rnum=2#19eb6da6a6ae89fd](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/6371773a2dba0892/19eb6da6a6ae89fd?q=itsme213+encoding&rnum=2#19eb6da6a6ae89fd)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 1, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/immediate-values/85885/3 "2006-01-01T14:22:36Z")

</div>

On 1/1/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Dec 31, 2005, at 10:07 PM, Johannes F. wrote:
> 
> > So in  
> > a=4  
> > ‘a’ does not hold a reference, technically speaking, but rather the  
> > immediate value 4.
> 
> I know I’m being pedantic (again) but I’d rather think of assignment  
> as _always_ copying references. It is simpler that way.

You’re describing the user model, I was talking about the  
implementation. Immediate values are objects for (almost) all  
practical purposes, so whether you think of fixnums as references to  
immutable singleton objects or as immediate values doesn’t make much  
difference.  
And as you say, it’s simpler to use the same model for everything,  
which is why many languages go to quite some lengths to hide the  
difference.

I tend to think of programming languages (Ruby, C, assembler) as user  
intefaces for the computer hardware. That makes ‘usability’ an  
important factor for programming langauges, and supporting simple (but  
still powerful) user models for how the language works is one aspect  
of usability.  
Implementation details are IMO mostly relevant for explaining why  
there are two classes for integers (fixnum and bignum) in the first  
place, or for that matter why there are ‘integers’ and ‘floats’  
instead of simply ‘numbers’. In day-to-day programming, they rarely  
matter.

jf
