Forum: Ruby rb_hash_aref and symbol keys

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8b4249ca3bb8c123da9f7aca63a652e1?d=identicon&s=25 Andre Nathan (Guest)
on 2006-04-07 16:30
(Received via mailing list)
Hello

I'm just getting starting with ruby C extensions, and I'm having a
problem accessing hash values corresponding to keys which are symbols.
String keys work fine:

/* In ruby: myhash = { 'foo' => 'blah' } */
foo = rb_hash_aref(myhash, rb_str_new2("foo")); /* works */

/* In ruby: myhash = { :foo => 'blah' } */
foo = rb_hash_aref(myhash, rb_intern("foo")); /* returns Qnil :( */

What is the proper way to do that? I found someone with the same problem
in the archives, but I'm afraid I didn't understand Nobu's answer in
ruby-talk:86571 :\

Thanks in advance,
Andre
956f185be9eac1760a2a54e287c4c844?d=identicon&s=25 ts (Guest)
on 2006-04-07 16:42
(Received via mailing list)
>>>>> "A" == Andre Nathan <andre@digirati.com.br> writes:

A> /* In ruby: myhash = { :foo => 'blah' } */
A> foo = rb_hash_aref(myhash, rb_intern("foo")); /* returns Qnil :( */

   foo = rb_hash_aref(myhash, ID2SYM(rb_intern("foo")));


Guy Decoux
8b4249ca3bb8c123da9f7aca63a652e1?d=identicon&s=25 Andre Nathan (Guest)
on 2006-04-07 16:45
(Received via mailing list)
On Fri, 2006-04-07 at 23:42 +0900, ts wrote:
>    foo = rb_hash_aref(myhash, ID2SYM(rb_intern("foo")));

Thanks a lot!

Andre
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2006-04-07 17:04
(Received via mailing list)
Hi,

In message "Re: rb_hash_aref and symbol keys"
    on Fri, 7 Apr 2006 23:28:17 +0900, Andre Nathan
<andre@digirati.com.br> writes:

|/* In ruby: myhash = { :foo => 'blah' } */
|foo = rb_hash_aref(myhash, rb_intern("foo")); /* returns Qnil :( */

rb_intern() gives you ID, which is a mere C number.  You have to
convert it into a Ruby Symbol Object by using ID2SYM() macro.

							matz.
8b4249ca3bb8c123da9f7aca63a652e1?d=identicon&s=25 Andre Nathan (Guest)
on 2006-04-07 18:17
(Received via mailing list)
On Sat, 2006-04-08 at 00:02 +0900, Yukihiro Matsumoto wrote:
> rb_intern() gives you ID, which is a mere C number.  You have to
> convert it into a Ruby Symbol Object by using ID2SYM() macro.

Thank you Matz. Is this documented anywhere? It's not mentioned in
README.EXT.

Andre
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2006-04-07 19:09
(Received via mailing list)
In message "Re: rb_hash_aref and symbol keys"
    on Sat, 8 Apr 2006 01:16:48 +0900, Andre Nathan
<andre@digirati.com.br> writes:
|
|On Sat, 2006-04-08 at 00:02 +0900, Yukihiro Matsumoto wrote:
|> rb_intern() gives you ID, which is a mere C number.  You have to
|> convert it into a Ruby Symbol Object by using ID2SYM() macro.
|
|Thank you Matz. Is this documented anywhere? It's not mentioned in
|README.EXT.

I will update the following section in README.EXT:

2.2.2 ID or Symbol

You can invoke methods directly, without parsing the string.  First I
need
to explain about ID.  ID is the integer number to represent Ruby's
identifiers such as variable names.  The Ruby data type corresponding to
ID
is Symbol.  It can be accessed from Ruby in the form:

 :Identifier

You can get the ID value from a string within C code by using

  rb_intern(const char *name)

You can convert C ID to Ruby Symbol by using

  VALUE ID2SYM(ID id)

and to convert Ruby Symbol to ID, use

  ID SYM2ID(VALUE sym)
8b4249ca3bb8c123da9f7aca63a652e1?d=identicon&s=25 Andre Nathan (Guest)
on 2006-04-07 19:09
(Received via mailing list)
On Sat, 2006-04-08 at 02:07 +0900, Yukihiro Matsumoto wrote:
> I will update the following section in README.EXT:

Perfect! Thanks again :)

Andre
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2006-04-07 19:18
(Received via mailing list)
Hi,

In message "Re: rb_hash_aref and symbol keys"
    on Sat, 8 Apr 2006 02:09:03 +0900, Andre Nathan
<andre@digirati.com.br> writes:

|On Sat, 2006-04-08 at 02:07 +0900, Yukihiro Matsumoto wrote:
|> I will update the following section in README.EXT:
|
|Perfect! Thanks again :)

FYI, Ruby didn't have Symbol objects before.  It used to use Fixnums
instead of Symbols.  The description in README.EXT reflect the old
situation.

							matz.
This topic is locked and can not be replied to.