# Setting and initializing instance variables on RubyClasses

**URL:** <https://www.ruby-forum.com/t/setting-and-initializing-instance-variables-on-rubyclasses/191128>\
**Category:** IronRuby\
**Created:** [July 6, 2010, 7:59am UTC](https://www.ruby-forum.com/t/setting-and-initializing-instance-variables-on-rubyclasses/191128 "2010-07-06T07:59:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [July 6, 2010, 7:59am UTC](https://www.ruby-forum.com/t/setting-and-initializing-instance-variables-on-rubyclasses/191128/1 "2010-07-06T07:59:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [July 6, 2010, 8:38am UTC](https://www.ruby-forum.com/t/setting-and-initializing-instance-variables-on-rubyclasses/191128/2 "2010-07-06T08:38:28Z")

</div>

What is your overall goal? Are you implementing a Ruby native library?

Tomas

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [July 6, 2010, 8:55am UTC](https://www.ruby-forum.com/t/setting-and-initializing-instance-variables-on-rubyclasses/191128/3 "2010-07-06T08:55:27Z")

</div>

Wow, that was quick! Thanks Tomas.

If by Ruby native you mean mostly implemented in Ruby, then no.

I mentioned the Ruby snippets just for clarification. If I understand  
correctly, SetClassVariable has the same semantics as declaring what  
Ruby  
would call a class variable, as in @@some\_var. @@some\_var would be  
visible  
to the whole class hierarchy, so `FancyGraphics < Graphics; end` would  
share  
the same @@some\_var instance - which is _not_ what I’d like.

Just curious how that might work using the IR APIs.

I assume the second question made sense. If not, let me know.

Cheers,

Charles

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [July 7, 2010, 6:16pm UTC](https://www.ruby-forum.com/t/setting-and-initializing-instance-variables-on-rubyclasses/191128/4 "2010-07-07T18:16:40Z")

</div>

By native Ruby library/extension I meant an implementation in C.

Although possible it’s not easy to simulate Ruby classes definition in  
C# in a declarative way. I would suggest writing your implementation in  
Ruby and calling .NET when you need so using IronRuby’s .NET interop.  
If you absolutely need to implement your Ruby classes in C# (I would be  
interested to understand why) you can use C# instance fields instead of  
Ruby’s instance variables. They won’t be exposed as instance variables  
to Ruby but they would be faster to work with. The fact they are not  
exposed shouldn’t matter since instance variables should be a private  
implementation detail of the class anyways.

It is more complicated with per-class data. You shouldn’t use static  
fields since the value should be bound to Ruby runtime. To allocate data  
bound to a runtime we provide GetOrCreateLibraryData method on  
RubyContext. You’ll find a few places where it’s used in  
IronRuby.Library. But before you dig into that consider writing your  
code in Ruby and perhaps parts of it that are perf critical in pure C#  
(w/o Ruby specific attributes). Our C# interop is strong so there  
shouldn’t be generally a need for writing Ruby code in C#.

Tomas

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [July 25, 2010, 12:46pm UTC](https://www.ruby-forum.com/t/setting-and-initializing-instance-variables-on-rubyclasses/191128/5 "2010-07-25T12:46:21Z")

</div>

> By native Ruby library/extension I meant an implementation in C.

D’oh! I realized that’s what you’d meant after I replied. Woops :).

If you absolutely need to implement your Ruby classes in C# (I would be

> interested to understand why)

I probably don’t need to for the most part, but I do have some  
motivation  
for doing so. Performance is a must. Also, I’m doing a lot of  
WritableBitmap manipulations, using (among other things) the  
WritableBitmapEx on CodePlex, which feels a lot more natural in C#.  
I’ve  
already completed my little project using pure [Iron]Ruby and WinForms,  
but  
I figured I’d try out the IronRuby API & C# now that I’m porting it to  
Silverlight. The interop with plain vanilla C# libs is great, but I  
can’t  
picture pulling off stuff like #\_dump and .\_load without going the API  
route. Also, I think it’ll be good practice using the IronRuby API from  
C#,  
as I’d like to contribute back to libraries (ruby-debug-ide and ffi are  
at  
the top of my list).

To allocate data bound to a runtime we provide GetOrCreateLibraryData  
method

> on RubyContext.

I’ll have to check that out. For now, using the following pattern seems  
to  
work well enough:

// from the Graphics class  
self.TryGetClassVariable(“renderables”, out renderables) // where  
self is  
the RubyClass

// from another class:  
var graphicsClass = context.GetClass(typeof(Graphics));  
graphicsClass.TryGetClassVariable(“renderables”, out renderables)

I also (think) I saw some code throughout IronRuby that used  
CallSiteStorage  
to interact with ruby objects. Would that yield better performance?

Cheers,

-Charles

On Wed, Jul 7, 2010 at 11:15 AM, Tomas M. \<

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [July 25, 2010, 9:20pm UTC](https://www.ruby-forum.com/t/setting-and-initializing-instance-variables-on-rubyclasses/191128/6 "2010-07-25T21:20:51Z")

</div>

So you’re building a graphics library based on WritableBitmap?  
You can certainly try to build Ruby classes in C# however it might be  
much easier and more flexible to build basic data structures and  
algorithms that need to be highly optimized in regular C# (no Ruby  
specific stuff) and then wrap them into a Ruby class in Ruby code. Your  
C#.dll might define a regular C# class Graphics:

namespace MyNamespace {  
public class Graphics {  
public void DoStuff() { }  
}  
}

And then you’ll write a Ruby wrapper:

require ‘graphics.dll’  
class MyNamespace::Graphics  
def ruby\_facade  
do something ruby-ish  
do\_stuff  
end  
end

Maybe if you gave concrete examples of the APIs you’re designing it  
might help me understand why this wouldn’t be sufficient. The goal of  
IronRuby is to allow you to use interop with regular C# code easily and  
efficiently without a need to specialize it for Ruby. For calling back  
to Ruby you can use C#'s dynamic features. If there is anything that  
prevents you from doing so let us know. The additional advantage of  
this approach is that your C# code can easily be used from other  
languages as well.

Re: CallSiteStorage: any time you need to dynamically call a method  
using Ruby semantics you should use it to do so. Three are plenty of use  
cases throughout the library that you can see for examples (search for  
Storage).

We don’t have any optimizations in place for class variable access. So  
TryGetClassVariable always looks up a dictionary.

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Charles  
Strahan  
Sent: Sunday, July 25, 2010 3:02 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Setting and initializing instance variables  
on RubyClasses

By native Ruby library/extension I meant an implementation in C.

D’oh! I realized that’s what you’d meant after I replied. Woops :).

If you absolutely need to implement your Ruby classes in C# (I would be  
interested to understand why)

I probably don’t need to for the most part, but I do have some  
motivation for doing so. Performance is a must. Also, I’m doing a lot  
of WritableBitmap manipulations, using (among other things) the  
WritableBitmapEx on CodePlex, which feels a lot more natural in C#.  
I’ve already completed my little project using pure [Iron]Ruby and  
WinForms, but I figured I’d try out the IronRuby API & C# now that I’m  
porting it to Silverlight. The interop with plain vanilla C# libs is  
great, but I can’t picture pulling off stuff like #\_dump and .\_load  
without going the API route. Also, I think it’ll be good practice using  
the IronRuby API from C#, as I’d like to contribute back to libraries  
(ruby-debug-ide and ffi are at the top of my list).

To allocate data bound to a runtime we provide GetOrCreateLibraryData  
method on RubyContext.

I’ll have to check that out. For now, using the following pattern seems  
to work well enough:

// from the Graphics class  
self.TryGetClassVariable(“renderables”, out renderables) // where  
self is the RubyClass

// from another class:  
var graphicsClass = context.GetClass(typeof(Graphics));  
graphicsClass.TryGetClassVariable(“renderables”, out renderables)

I also (think) I saw some code throughout IronRuby that used  
CallSiteStorage to interact with ruby objects. Would that yield better  
performance?

Cheers,

-Charles

On Wed, Jul 7, 2010 at 11:15 AM, Tomas M.  
\<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\>  
wrote:  
By native Ruby library/extension I meant an implementation in C.

Although possible it’s not easy to simulate Ruby classes definition in  
C# in a declarative way. I would suggest writing your implementation in  
Ruby and calling .NET when you need so using IronRuby’s .NET interop.  
If you absolutely need to implement your Ruby classes in C# (I would be  
interested to understand why) you can use C# instance fields instead of  
Ruby’s instance variables. They won’t be exposed as instance variables  
to Ruby but they would be faster to work with. The fact they are not  
exposed shouldn’t matter since instance variables should be a private  
implementation detail of the class anyways.

It is more complicated with per-class data. You shouldn’t use static  
fields since the value should be bound to Ruby runtime. To allocate data  
bound to a runtime we provide GetOrCreateLibraryData method on  
RubyContext. You’ll find a few places where it’s used in  
IronRuby.Library. But before you dig into that consider writing your  
code in Ruby and perhaps parts of it that are perf critical in pure C#  
(w/o Ruby specific attributes). Our C# interop is strong so there  
shouldn’t be generally a need for writing Ruby code in C#.

Tomas

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [July 26, 2010, 2:51pm UTC](https://www.ruby-forum.com/t/setting-and-initializing-instance-variables-on-rubyclasses/191128/7 "2010-07-26T14:51:31Z")

</div>

It’s actually an entire game engine port :).

[http://www.rpgmakerweb.com/product.html?prod=xp](http://www.rpgmakerweb.com/product.html?prod=xp)

However, I don’t believe “port” is the correct word - perhaps  
“reimplementation” would be more accurate. I don’t have access to RPG  
Maker’s source code, so I’ve written the entire game engine from the  
documented APIs, and by injecting little bits of Ruby into RPG Maker’s  
Ruby  
runtime to fill in any of the blanks. Finally, I ran several games  
through  
my “port” and fixed any little bugs I came across. It was no small  
feat, I  
assure you :).

So, as you can probably imagine, the rendering is highly coupled to the  
rest  
of the engine. As a result, I have no expectations to use it anywhere  
else. Sure, I could abstract away stuff like drawing rectangles and  
circles  
to bitmaps, but anything more than that (sprites, windows, etc.)  
wouldn’t  
make any sense outside of this particular game engine. I’m sure I could  
do  
it all in Ruby (and have, when I wrote the WinForms impl) - but I don’t  
find  
IronRuby as easy to debug as C# (yet). So, if I’m going to write any C#  
code, that C# code will need to directly interop with the rest of the  
Ruby  
modules and classes (in most cases). I’m sure the IronRuby APIs will  
change  
from version to version, but that’s not a big deal in my case. I’m fine  
updating my code to match the latest release, or just tying my code to a  
specific version. My current goal is to make my port run on Silverlight  
and, consequently, Windows Phone 7 so that people can redistribute their  
games on the market place or on the web; in both cases, targeting a  
specific  
version of IronRuby is easy. If, on the otherhand, I wanted to  
distribute a  
platform agnostic implementation, then I would run into problems as  
there’s  
no telling what version of IronRuby is installed on each computer.

In case you’re curious about the legality of my project, I assure you  
that  
it’s quite fine. The only thing I plan to redistribute is some code  
that  
will run existing games. No copyright protected materials such as  
images,  
audio, video etc will be redistributed - nor do I have any interest in  
developing the part of the RPG Maker software that actually let’s you  
_make_games, which means that my project won’t impact Enterbrain’s  
profits in any  
way (their EULA allows the customer to redistribute their games however  
the  
customer sees fit, so they only make money when someone purchases RPG  
Maker  
to develop games).

If you’re still interested in taking a peek at my code, I plan on  
distributing the source on GitHub some time soon.

-Charles

On Sun, Jul 25, 2010 at 1:50 PM, Tomas M. \<

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [July 26, 2010, 6:01pm UTC](https://www.ruby-forum.com/t/setting-and-initializing-instance-variables-on-rubyclasses/191128/8 "2010-07-26T18:01:48Z")

</div>

Cool! 🙂

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Charles  
Strahan  
Sent: Monday, July 26, 2010 5:24 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Setting and initializing instance variables  
on RubyClasses

It’s actually an entire game engine port :).

[http://www.rpgmakerweb.com/product.html?prod=xp](http://www.rpgmakerweb.com/product.html?prod=xp)

However, I don’t believe “port” is the correct word - perhaps  
“reimplementation” would be more accurate. I don’t have access to RPG  
Maker’s source code, so I’ve written the entire game engine from the  
documented APIs, and by injecting little bits of Ruby into RPG Maker’s  
Ruby runtime to fill in any of the blanks. Finally, I ran several games  
through my “port” and fixed any little bugs I came across. It was no  
small feat, I assure you :).

So, as you can probably imagine, the rendering is highly coupled to the  
rest of the engine. As a result, I have no expectations to use it  
anywhere else. Sure, I could abstract away stuff like drawing  
rectangles and circles to bitmaps, but anything more than that (sprites,  
windows, etc.) wouldn’t make any sense outside of this particular game  
engine. I’m sure I could do it all in Ruby (and have, when I wrote the  
WinForms impl) - but I don’t find IronRuby as easy to debug as C# (yet).  
So, if I’m going to write any C# code, that C# code will need to  
directly interop with the rest of the Ruby modules and classes (in most  
cases). I’m sure the IronRuby APIs will change from version to version,  
but that’s not a big deal in my case. I’m fine updating my code to  
match the latest release, or just tying my code to a specific version.  
My current goal is to make my port run on Silverlight and, consequently,  
Windows Phone 7 so that people can redistribute their games on the  
market place or on the web; in both cases, targeting a specific version  
of IronRuby is easy. If, on the otherhand, I wanted to distribute a  
platform agnostic implementation, then I would run into problems as  
there’s no telling what version of IronRuby is installed on each  
computer.

In case you’re curious about the legality of my project, I assure you  
that it’s quite fine. The only thing I plan to redistribute is some  
code that will run existing games. No copyright protected materials  
such as images, audio, video etc will be redistributed - nor do I have  
any interest in developing the part of the RPG Maker software that  
actually let’s you make games, which means that my project won’t impact  
Enterbrain’s profits in any way (their EULA allows the customer to  
redistribute their games however the customer sees fit, so they only  
make money when someone purchases RPG Maker to develop games).

If you’re still interested in taking a peek at my code, I plan on  
distributing the source on GitHub some time soon.

-Charles

On Sun, Jul 25, 2010 at 1:50 PM, Tomas M.  
\<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\>  
wrote:  
So you’re building a graphics library based on WritableBitmap?  
You can certainly try to build Ruby classes in C# however it might be  
much easier and more flexible to build basic data structures and  
algorithms that need to be highly optimized in regular C# (no Ruby  
specific stuff) and then wrap them into a Ruby class in Ruby code. Your  
C#.dll might define a regular C# class Graphics:

namespace MyNamespace {  
public class Graphics {  
public void DoStuff() { }  
}  
}

And then you’ll write a Ruby wrapper:

require ‘graphics.dll’  
class MyNamespace::Graphics  
def ruby\_facade  
do something ruby-ish  
do\_stuff  
end  
end

Maybe if you gave concrete examples of the APIs you’re designing it  
might help me understand why this wouldn’t be sufficient. The goal of  
IronRuby is to allow you to use interop with regular C# code easily and  
efficiently without a need to specialize it for Ruby. For calling back  
to Ruby you can use C#'s dynamic features. If there is anything that  
prevents you from doing so let us know. The additional advantage of  
this approach is that your C# code can easily be used from other  
languages as well.

Re: CallSiteStorage: any time you need to dynamically call a method  
using Ruby semantics you should use it to do so. Three are plenty of use  
cases throughout the library that you can see for examples (search for  
Storage).

We don’t have any optimizations in place for class variable access. So  
TryGetClassVariable always looks up a dictionary.

Tomas

From:  
removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
On Behalf Of Charles S.  
Sent: Sunday, July 25, 2010 3:02 AM

To: removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Ironruby-core] Setting and initializing instance variables  
on RubyClasses

By native Ruby library/extension I meant an implementation in C.

D’oh! I realized that’s what you’d meant after I replied. Woops :).  
If you absolutely need to implement your Ruby classes in C# (I would be  
interested to understand why)

I probably don’t need to for the most part, but I do have some  
motivation for doing so. Performance is a must. Also, I’m doing a lot  
of WritableBitmap manipulations, using (among other things) the  
WritableBitmapEx on CodePlex, which feels a lot more natural in C#.  
I’ve already completed my little project using pure [Iron]Ruby and  
WinForms, but I figured I’d try out the IronRuby API & C# now that I’m  
porting it to Silverlight. The interop with plain vanilla C# libs is  
great, but I can’t picture pulling off stuff like #\_dump and .\_load  
without going the API route. Also, I think it’ll be good practice using  
the IronRuby API from C#, as I’d like to contribute back to libraries  
(ruby-debug-ide and ffi are at the top of my list).  
To allocate data bound to a runtime we provide GetOrCreateLibraryData  
method on RubyContext.

I’ll have to check that out. For now, using the following pattern seems  
to work well enough:

// from the Graphics class  
self.TryGetClassVariable(“renderables”, out renderables) // where  
self is the RubyClass

// from another class:  
var graphicsClass = context.GetClass(typeof(Graphics));  
graphicsClass.TryGetClassVariable(“renderables”, out renderables)

I also (think) I saw some code throughout IronRuby that used  
CallSiteStorage to interact with ruby objects. Would that yield better  
performance?

Cheers,

-Charles  
On Wed, Jul 7, 2010 at 11:15 AM, Tomas M.  
\<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\>  
wrote:  
By native Ruby library/extension I meant an implementation in C.

Although possible it’s not easy to simulate Ruby classes definition in  
C# in a declarative way. I would suggest writing your implementation in  
Ruby and calling .NET when you need so using IronRuby’s .NET interop.  
If you absolutely need to implement your Ruby classes in C# (I would be  
interested to understand why) you can use C# instance fields instead of  
Ruby’s instance variables. They won’t be exposed as instance variables  
to Ruby but they would be faster to work with. The fact they are not  
exposed shouldn’t matter since instance variables should be a private  
implementation detail of the class anyways.

It is more complicated with per-class data. You shouldn’t use static  
fields since the value should be bound to Ruby runtime. To allocate data  
bound to a runtime we provide GetOrCreateLibraryData method on  
RubyContext. You’ll find a few places where it’s used in  
IronRuby.Library. But before you dig into that consider writing your  
code in Ruby and perhaps parts of it that are perf critical in pure C#  
(w/o Ruby specific attributes). Our C# interop is strong so there  
shouldn’t be generally a need for writing Ruby code in C#.

Tomas
