Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "VALUE" type in Ruby source files?

Tags:

c

ruby

I'm quite new to Ruby, but when I was searching the documentation of Ruby, I found some C source code too which was included in the documentation. In almost all of those references, I found the VALUE and I'm quite sure its a struct. But what are the attributes of this VALUE struct and what is this actually doing?

like image 837
Sazid Avatar asked May 07 '13 13:05

Sazid


2 Answers

You can see the definition for VALUE in the header file ruby/include/ruby/internal/value.h:

#if defined HAVE_UINTPTR_T && 0
typedef uintptr_t VALUE;
...
#elif SIZEOF_LONG == SIZEOF_VOIDP
typedef unsigned long VALUE;
...    
#else
# error
#endif

So value is just a pointer to an object. As described in Priti's link, there is a TYPE() macro in the same file that can be used to identify the type of data pointed to by a VALUE.

like image 152
Justin Ethier Avatar answered Sep 23 '22 16:09

Justin Ethier


In C, variables have types and data do not have types. In contrast, Ruby variables do not have a static type, and data themselves have types, so data will need to be converted between the languages.

Data in Ruby are represented by the C type `VALUE'. Each VALUE data has its data-type.

To retrieve C data from a VALUE, you need to:

1. Identify the VALUE's data type

2. Convert the VALUE into C data

For more information look here: Basic Knowledge

like image 42
Arup Rakshit Avatar answered Sep 19 '22 16:09

Arup Rakshit