# How to get the key, value pairs of RHASH?

**URL:** <https://www.ruby-forum.com/t/how-to-get-the-key-value-pairs-of-rhash/112311>\
**Category:** Ruby\
**Created:** [August 17, 2007, 11:01am UTC](https://www.ruby-forum.com/t/how-to-get-the-key-value-pairs-of-rhash/112311 "2007-08-17T11:01:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 17, 2007, 11:01am UTC](https://www.ruby-forum.com/t/how-to-get-the-key-value-pairs-of-rhash/112311/1 "2007-08-17T11:01:21Z")

</div>

i’d like to have, in my module options (is in FileUtils for ex) like  
that in the Ruby side:

symt = SYMBOL\_TEST.new( “/my/src/path”, :verbose =\> true, :noop =\>  
true )

i know, from experiment, that, in my C init i get 2 VALUES in that  
case the first being a RString the second an RHash.

if i look at ruby.h i get :

struct RHash {  
struct RBasic basic;  
struct st\_table \*tbl;  
int iter\_lev;  
VALUE ifnone;  
};

i didn’t found anything about “struct st\_table \*tbl” i imagine that’s  
here the key/value pairs are stored ???

but how to retrieve them ???

generally speaking where could i find examples for each type of  
struct ???

because i’ll need in the near future the same kind of knoledge about :

struct RRegexp {  
struct RBasic basic;  
struct re\_pattern\_buffer \*ptr;  
long len;  
char \*str;  
};

struct RFile {  
struct RBasic basic;  
struct OpenFile \*fptr;  
};

struct RData {  
struct RBasic basic;  
void (_dmark)(void_);  
void (_dfree)(void_);  
void \*data;  
};

best,

Yvon

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 17, 2007, 11:17am UTC](https://www.ruby-forum.com/t/how-to-get-the-key-value-pairs-of-rhash/112311/2 "2007-08-17T11:17:22Z")

</div>

On Aug 17, 2007, at 01:59, unbewust wrote:

> but how to retrieve them ???

Use rb\_hash\_aset and rb\_hash\_aref from hash.c. Jump down to the  
Init\_Hash() to get a list of what you can do.

> generally speaking where could i find examples for each type of  
> struct ???

The pickaxe (including first edition) has a section on this. The C  
API hasn’t changed much.

[http://www.rubycentral.com/pickaxe/ext\_ruby.html](http://www.rubycentral.com/pickaxe/ext_ruby.html)

> because i’ll need in the near future the same kind of knoledge about :
> 
> struct RRegexp {  
> struct RBasic basic;  
> struct re\_pattern\_buffer \*ptr;  
> long len;  
> char \*str;  
> };

re.c

> struct RFile {  
> struct RBasic basic;  
> struct OpenFile \*fptr;  
> };

io.c and file.c

> struct RData {  
> struct RBasic basic;  
> void (_dmark)(void_);  
> void (_dfree)(void_);  
> void \*data;  
> };

I think the pickaxe has a section on this.

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 17, 2007, 1:11pm UTC](https://www.ruby-forum.com/t/how-to-get-the-key-value-pairs-of-rhash/112311/3 "2007-08-17T13:11:38Z")

</div>

On 17 août, 11:16, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > i know, from experiment, that, in my C init i get 2 VALUES in that
> 
> > ```
> > long len;
> > 
> > ```
> 
> ## io.c and file.c
> 
> Poor workers blame their tools. Good workers build better tools. The  
> best workers get their tools to do the work for them. – Syndicate Wars

thanks a lot !

Yvon

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/how-to-get-the-key-value-pairs-of-rhash/112311/4 "2007-09-25T23:04:57Z")

</div>

On 8/17/07, unbewust [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> way to produce a Ruby symbol (as :verbose) from C.
> 
> thought i’ve seen ID2SYM and SYM2ID in ruby.h
> 
> also not all of the rb\_xxx methods found in hash.c are working from C,  
> surprisingly ???

If any of the methods in hash.c are preceded by the keyword “static”,  
then they cannot be called from outside the hash.c file.

> for example :
> 
> ~/work/C/Cext2Ruby/SYMBOL\_TEST/ext%\> ruby sample.rb  
> dyld: NSLinkModule() error  
> dyld: Symbol not found: \_rb\_hash\_keys  
> Referenced from: ./symbol\_test.bundle  
> Expected in: flat namespace

VALUE keys = rb\_funcall( hash, rb\_intern( “keys” ), 0 );

> ~/work/C/Cext2Ruby/SYMBOL\_TEST/ext%\> ruby sample.rb  
> dyld: NSLinkModule() error  
> dyld: Symbol not found: \_rb\_hash\_size  
> Referenced from: ./symbol\_test.bundle  
> Expected in: flat namespace

long size = RHASH(hash)-\>tbl-\>num\_entries;

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 17, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/how-to-get-the-key-value-pairs-of-rhash/112311/5 "2007-08-17T17:21:04Z")

</div>

On 17 août, 11:16, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > but how to retrieve them ???
> 
> Use rb\_hash\_aset and rb\_hash\_aref from hash.c. Jump down to the  
> Init\_Hash() to get a list of what you can do.

it’s OK now for hash keys being strings NOT symbol, i did’nt found a  
way to produce a Ruby symbol (as :verbose) from C.

thought i’ve seen ID2SYM and SYM2ID in ruby.h

also not all of the rb\_xxx methods found in hash.c are working from C,  
surprisingly ???

for example :

~/work/C/Cext2Ruby/SYMBOL\_TEST/ext%\> ruby sample.rb  
dyld: NSLinkModule() error  
dyld: Symbol not found: \_rb\_hash\_keys  
Referenced from: ./symbol\_test.bundle  
Expected in: flat namespace

~/work/C/Cext2Ruby/SYMBOL\_TEST/ext%\> ruby sample.rb  
dyld: NSLinkModule() error  
dyld: Symbol not found: \_rb\_hash\_size  
Referenced from: ./symbol\_test.bundle  
Expected in: flat namespace

the Symbol for “rb\_hash\_keys” or “rb\_hash\_size” aren’t found in my  
bundle ???

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [September 25, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/how-to-get-the-key-value-pairs-of-rhash/112311/6 "2007-09-25T23:09:59Z")

</div>

On 17 août, 17:01, “Tim P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 8/17/07, unbewust [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If any of the methods in hash.c are preceded by the keyword “static”,  
> then they cannot be called from outside the hash.c file.

> long size = RHASH(hash)-\>tbl-\>num\_entries;

OK, thanks again and again u’r a hole of science 😉

best,

Yvon
