I am confused with Value Type Objects

S

Sam Sungshik Kong

Hello!

In ruby, every object is reference type except for FixNum, NilClass,
TrueClass, FalseClass.
Let's consider FixNum only.

According to http://www.rubygarden.org/ruby?GCAndMemoryManagement, FixNum
takes only 4 bytes (on 32-bit machines).
Is it true?
If so, doesn't it have any other information except for the value itself?
How does ruby know that a FixNum is actually a FixNum instead of a reference
to an object when it doesn't have any information about the type?

I'm from C# background.
In C# (actually dotnet framework), values types are structures not just a
simple value.

Could somebody resolve my confusion?

Thanks.

Sam
 
A

Ara.T.Howard

Hello!

In ruby, every object is reference type except for FixNum, NilClass,
TrueClass, FalseClass.
Let's consider FixNum only.

According to http://www.rubygarden.org/ruby?GCAndMemoryManagement, FixNum
takes only 4 bytes (on 32-bit machines).
Is it true?
If so, doesn't it have any other information except for the value itself?
How does ruby know that a FixNum is actually a FixNum instead of a reference
to an object when it doesn't have any information about the type?

I'm from C# background.
In C# (actually dotnet framework), values types are structures not just a
simple value.

Could somebody resolve my confusion?

Thanks.

Sam

only 31 bits. 1 bit is used as a flag to say "this is a ptr to object or it
is an immediate value stored in word"

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
M

Mauricio Fernández

According to http://www.rubygarden.org/ruby?GCAndMemoryManagement, FixNum
takes only 4 bytes (on 32-bit machines).
Is it true?
If so, doesn't it have any other information except for the value itself?
How does ruby know that a FixNum is actually a FixNum instead of a reference
to an object when it doesn't have any information about the type?

In Ruby, Fixnums have 31 bits; the missing bit (least significant one)
is used to mark it as a fixnum. This works because other objects will
be at even addresses.
I'm from C# background.
In C# (actually dotnet framework), values types are structures not just a
simple value.

Most objects in Ruby are indeed represented internally by structures;
take a look at ruby.h.

--
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

We are using Linux daily to UP our productivity - so UP yours!
-- Adapted from Pat Paulsen by Joe Sloan
 
M

Mark Sparshatt

Sam said:
Hello!

In ruby, every object is reference type except for FixNum, NilClass,
TrueClass, FalseClass.
Let's consider FixNum only.

According to http://www.rubygarden.org/ruby?GCAndMemoryManagement, FixNum
takes only 4 bytes (on 32-bit machines).
Is it true?
If so, doesn't it have any other information except for the value itself?
How does ruby know that a FixNum is actually a FixNum instead of a reference
to an object when it doesn't have any information about the type?

I'm from C# background.
In C# (actually dotnet framework), values types are structures not just a
simple value.

Could somebody resolve my confusion?
Ruby checks the least significant bit of the value. If this is set then
the value is a direct value such as a fixnum, otherwise it's a reference
to an object.

When Ruby is constructing a fixnum it left shifts the number one place
then sets the LSB.

HTH
 
S

Sam Sungshik Kong

Wow, I'm so impressed that within 30 minutes I got 3 answers...:)
This group is very active, helpful, and enthusiastic.
Thanks all.

May I ask further questions related to the direct value type objects?

Now I understand how the FixNum is stored in the memory.
What about other value types - true, false, nil?
How are they distinguished from FixNum?
What are their sized?

Sorry to bother you guys with my beginner questions.

Sam
 
M

Mauricio Fernández

Now I understand how the FixNum is stored in the memory.
What about other value types - true, false, nil?

They are defined as the following VALUEs:

/* special contants - i.e. non-zero and non-fixnum constants */
#define Qfalse 0
#define Qtrue 2
#define Qnil 4

and still use 32 bits.


--
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

'Ooohh.. "FreeBSD is faster over loopback, when compared to Linux
over the wire". Film at 11.'
-- Linus Torvalds
 

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,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top