I am confused with Value Type Objects

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

···

On Fri, 25 Jun 2004, Sam Sungshik Kong wrote:

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

--

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

===============================================================================

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.

···

On Sat, Jun 26, 2004 at 02:38:05AM +0900, Sam Sungshik Kong wrote:

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

Sam Sungshik Kong wrote:

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

···

--
Mark Sparshatt

Wow, I'm so impressed that within 30 minutes I got 3 answers...:slight_smile:
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

"Sam Sungshik Kong" <ssk@chol.nospam.net> wrote in message
news:nmZCc.1209$QR1.961@newssvr27.news.prodigy.com...

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

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.

···

On Sat, Jun 26, 2004 at 03:13:08AM +0900, Sam Sungshik Kong wrote:

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

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