ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:20129] Ruby class variable access from C
@ 2008-11-26 19:49 Christopher Thompson
  2008-11-27  0:09 ` [ruby-core:20135] " Yukihiro Matsumoto
  2008-11-27  6:58 ` [ruby-core:20138] " _why
  0 siblings, 2 replies; 5+ messages in thread
From: Christopher Thompson @ 2008-11-26 19:49 UTC (permalink / raw
  To: ruby-core

I'm probably missing something trivial, but given the following Ruby code:

class Myclass
  @@myvar = 1

  def self.myvar
    return @@myvar
  end
end

I can access @@myvar from Ruby with Myclass::myvar.

Great, but now I need to access that same variable through a C
extension.  I think I can get it by doing:
VALUE myvar = rb_eval_string("Myclass::myvar");
but surely there's a better way?  I'm thinking something like
rb_cvar_get() but that requires that I pass in a VALUE and all I have is
a string at this point, "Myclass".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:20135] Re: Ruby class variable access from C
  2008-11-26 19:49 [ruby-core:20129] Ruby class variable access from C Christopher Thompson
@ 2008-11-27  0:09 ` Yukihiro Matsumoto
  2008-11-27  6:58 ` [ruby-core:20138] " _why
  1 sibling, 0 replies; 5+ messages in thread
From: Yukihiro Matsumoto @ 2008-11-27  0:09 UTC (permalink / raw
  To: ruby-core

Hi,

In message "Re: [ruby-core:20129] Ruby class variable access from C"
    on Thu, 27 Nov 2008 04:49:32 +0900, Christopher Thompson <cthompson@nexopia.com> writes:

|Great, but now I need to access that same variable through a C
|extension.  I think I can get it by doing:
|VALUE myvar = rb_eval_string("Myclass::myvar");
|but surely there's a better way?  I'm thinking something like
|rb_cvar_get() but that requires that I pass in a VALUE and all I have is
|a string at this point, "Myclass".

Get the reference of "Myclass" first.  You can get it by:

  * rb_eval_string()
  * rb_const_get()
  * or lot of other ways fit for your program

							matz.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:20138] Re: Ruby class variable access from C
  2008-11-26 19:49 [ruby-core:20129] Ruby class variable access from C Christopher Thompson
  2008-11-27  0:09 ` [ruby-core:20135] " Yukihiro Matsumoto
@ 2008-11-27  6:58 ` _why
  2008-11-27 16:27   ` [ruby-core:20139] " Christopher Thompson
  1 sibling, 1 reply; 5+ messages in thread
From: _why @ 2008-11-27  6:58 UTC (permalink / raw
  To: ruby-core

On Thu, Nov 27, 2008 at 04:49:32AM +0900, Christopher Thompson wrote:
> I think I can get it by doing:
> VALUE myvar = rb_eval_string("Myclass::myvar");
> but surely there's a better way?  I'm thinking something like
> rb_cvar_get() but that requires that I pass in a VALUE and all I have is
> a string at this point, "Myclass".

If you create the class in your C extension, you'll get a VALUE
back.

  VALUE cMyclass;

  void
  Init_myext()
  {
    cMyclass = rb_define_class("Myclass", rb_cObject);
  }

  VALUE
  much_much_later(VALUE self)
  {
    VALUE myvar = rb_cvar_get(cMyclass, rb_intern("@@myvar"));
  }

_why

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:20139] Re: Ruby class variable access from C
  2008-11-27  6:58 ` [ruby-core:20138] " _why
@ 2008-11-27 16:27   ` Christopher Thompson
  2008-11-27 17:39     ` [ruby-core:20141] " Jacob Fugal
  0 siblings, 1 reply; 5+ messages in thread
From: Christopher Thompson @ 2008-11-27 16:27 UTC (permalink / raw
  To: ruby-core

_why wrote:
> On Thu, Nov 27, 2008 at 04:49:32AM +0900, Christopher Thompson wrote:
>> I think I can get it by doing:
>> VALUE myvar = rb_eval_string("Myclass::myvar");
>> but surely there's a better way?  I'm thinking something like
>> rb_cvar_get() but that requires that I pass in a VALUE and all I have is
>> a string at this point, "Myclass".
> 
> If you create the class in your C extension, you'll get a VALUE
> back.
> 
>   VALUE cMyclass;
> 
>   void
>   Init_myext()
>   {
>     cMyclass = rb_define_class("Myclass", rb_cObject);
>   }
> 
>   VALUE
>   much_much_later(VALUE self)
>   {
>     VALUE myvar = rb_cvar_get(cMyclass, rb_intern("@@myvar"));
>   }
> 
> _why
> 

Ahh, but I'm not creating it in the C extension, for reasons that won't
be immediately obvious and possibly should be refactored.

Matz suggested rb_const_get and rb_eval_string.  rb_eval_string is what
I'm already doing, so I'll look into rb_const_get.  But thanks, your
approach would work for most sane extensions.  :)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:20141] Re: Ruby class variable access from C
  2008-11-27 16:27   ` [ruby-core:20139] " Christopher Thompson
@ 2008-11-27 17:39     ` Jacob Fugal
  0 siblings, 0 replies; 5+ messages in thread
From: Jacob Fugal @ 2008-11-27 17:39 UTC (permalink / raw
  To: ruby-core

On Thu, Nov 27, 2008 at 9:27 AM, Christopher Thompson
<cthompson@nexopia.com> wrote:
>> On Thu, Nov 27, 2008 at 04:49:32AM +0900, Christopher Thompson wrote:
>>> I think I can get it by doing:
>>> VALUE myvar = rb_eval_string("Myclass::myvar");
>>> but surely there's a better way?  I'm thinking something like
>>> rb_cvar_get() but that requires that I pass in a VALUE and all I have is
>>> a string at this point, "Myclass".
<snip>
>
> Matz suggested rb_const_get and rb_eval_string.  rb_eval_string is what
> I'm already doing, so I'll look into rb_const_get.  But thanks, your
> approach would work for most sane extensions.  :)

To elaborate on Matz' suggestion, I'd do something like:

VALUE klass = rb_const_get("Myclass");
VALUE myvar = rb_cvar_get(klass, "myvar");

Excuse any mistake in my usage of either of those methods, I haven't
actually used them in quite some time and didn't bother looking at the
documentation. :)

Jacob Fugal

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-11-27 17:48 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-26 19:49 [ruby-core:20129] Ruby class variable access from C Christopher Thompson
2008-11-27  0:09 ` [ruby-core:20135] " Yukihiro Matsumoto
2008-11-27  6:58 ` [ruby-core:20138] " _why
2008-11-27 16:27   ` [ruby-core:20139] " Christopher Thompson
2008-11-27 17:39     ` [ruby-core:20141] " Jacob Fugal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).