[Ironruby-core] Setting and initializing instance variables on RubyClasses
Tomas Matousek
Tomas.Matousek at microsoft.com
Tue Jul 6 02:17:22 EDT 2010
What is your overall goal? Are you implementing a Ruby native library?
Tomas
-----Original Message-----
From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Charles Strahan
Sent: Monday, July 05, 2010 10:59 PM
To: ironruby-core at rubyforge.org
Subject: [Ironruby-core] Setting and initializing instance variables on RubyClasses
Hello,
How can I go about setting an instance variable on a RubyClass?
Here's some code for context:
[RubyClass("Graphics")]
public class Graphics
{
[RubyMethod("frame_rate", RubyMethodAttributes.PublicSingleton)]
public static int GetFrameRate(RubyClass self)
{
// What goes here?
// I could use self.TryGetClassVariable, but that's a *class* variable, correct?
// I want to achieve the same thing as "return @frame_rate",
// not "return @@frame_rate"
}
[RubyMethod("frame_rate=", RubyMethodAttributes.PublicSingleton)]
public static void SetFrameRate(RubyClass self, int frameRate)
{
// Same thing here. I could do this:
// self.SetClassVariable("frame_rate", frameRate);
// but I want to achieve the same as this:
// @frame_rate = frame_rate
}
}
Another option *just* dawned on me; how about these two:
self.Context.SetInstanceVariable(self, "frame_rate", frameRate); self.Context.TryGetInstanceVariable(self, "frame_rate", out frameRate);
Is that what I'm looking for?
One more question: is there a way to get the library initializer to invoke a callback so that I may perform some initialization for the RubyClass itself? In the case above, I'd like to initialize the frame_rate when the RubyClass is created. As an example:
class Graphics
attr_accessor :frame_rate
frame_rate = 40
end
...
Graphics.frame_rate = something_else
I'd like that "frame_rate = 40" to happen when the assembly is loaded by the IronRuby runtime, if possible. I'm sure I could type that into my auto-generated LibraryInitializer, but I'd rather do this declaratively, perhaps using Attributes (like RubyMethod and RubyClass, etc).
Thanks,
-Charles
_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org
http://rubyforge.org/mailman/listinfo/ironruby-core
More information about the Ironruby-core
mailing list