# Accessing a Module's Classes from C# with dynamic

**URL:** <https://www.ruby-forum.com/t/accessing-a-modules-classes-from-c-with-dynamic/201755>\
**Category:** IronRuby\
**Created:** [January 13, 2011, 9:51pm UTC](https://www.ruby-forum.com/t/accessing-a-modules-classes-from-c-with-dynamic/201755 "2011-01-13T21:51:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [January 13, 2011, 9:51pm UTC](https://www.ruby-forum.com/t/accessing-a-modules-classes-from-c-with-dynamic/201755/1 "2011-01-13T21:51:48Z")

</div>

Hey guys.

I am currently experimenting with IronRuby as an extensibility solution  
for my application. However, I have hit a roadblock. What I would like  
to do is call Runtime.UseFile(“script.rb”), then access a global module  
in the script, and instantiate a specific class in that module.

Here is the script file for reference:  
module RbScriptApp  
class Foo  
def Bar()  
return (rand(100) + 1).to\_s();  
end  
end  
end

Initially, I thought it would be as easy as typing  
dynamic globals = myRuntime.Globals;  
dynamic myClass = globals.RbScriptApp.Foo.@new();  
myClass.Bar();  
…

but I soon found out this was not the case, as the builtin RubyModule  
class does not support this dynamic syntax. Is there a way around this  
or is it just plain impossible? I was unable to find _any_ information  
about how to do this (not even about RubyModule itself!) through Google.

The exception in question is a RuntimeBinderException:  
‘IronRuby.Builtins.RubyModule’ does not contain a definition for ‘Foo’

Right now my workaround is a call to RubyModule.EnumerateConstants and  
manual lookup of the class name I want, but it doesn’t have the  
syntactic elegance of the dynamic keyword… Any thoughts?

EDIT: Just thought I’d add, I’d like to stay away from Engine.Execute()  
if at all possible, unless someone can give me a reasonable case for not  
doing so.

Thanks.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [January 15, 2011, 8:55am UTC](https://www.ruby-forum.com/t/accessing-a-modules-classes-from-c-with-dynamic/201755/2 "2011-01-15T08:55:08Z")

</div>

It turns out that the problem is not accessing a module’s class, but  
accessing the contents of a module in a require’d file.

global.rb  
require ‘app’

app.rb  
module RbScriptApp  
class Foo  
def Bar()  
return (rand(100) + 1).to\_s()  
end  
end  
end

No matter what I do I can only access RbScriptApp and not Foo if I call  
Runtime.UseFile(“global.rb”). So far I have two other work arounds: 1.)  
create wrapper methods for everything I want to access in app.rb (ugh),  
or 2.) use Engine.Execute(“RbScriptApp::Foo.new”). The latter is more  
preferrable, I suppose, but it seems strange that I can’t access Foo by  
dynamic.

Any more information about this issue would be very helpful and  
appreciated.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [January 15, 2011, 2:08pm UTC](https://www.ruby-forum.com/t/accessing-a-modules-classes-from-c-with-dynamic/201755/3 "2011-01-15T14:08:42Z")

</div>

On Jan 15, 2011, at 2:55 AM, Joshua M. wrote:

> No matter what I do I can only access RbScriptApp and not Foo if I call  
> Runtime.UseFile(“global.rb”). So far I have two other work arounds: 1.)  
> create wrapper methods for everything I want to access in app.rb (ugh),  
> or 2.) use Engine.Execute(“RbScriptApp::Foo.new”). The latter is more  
> preferrable, I suppose, but it seems strange that I can’t access Foo by  
> dynamic.

That’s how Modules work in Ruby. Just like namespaces in C#.

Try adding

include RbScriptApp

to your global.rb file. You need to include the Module if you want to  
access the class without qualifying it.

> Any more information about this issue would be very helpful and  
> appreciated.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Ironruby-core mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

–  
Will G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [January 15, 2011, 10:15pm UTC](https://www.ruby-forum.com/t/accessing-a-modules-classes-from-c-with-dynamic/201755/4 "2011-01-15T22:15:59Z")

</div>

> You need to include the Module if you want to  
> access the class without qualifying it.

I have tried this but it doesn’t work (RuntimeBinderException:  
‘Microsoft.Scripting.Hosting.ScriptScope’ does not contain a definition  
for ‘Foo’).

In my original post I had specified the fully qualified name.

> RbScriptApp.const\_get(“Foo”)

InvalidOperationException: Empty scope has no global scope.

> I haven’t tried that, but I think it should work. Neither solution is  
> indeed ideal. CLR interop with Modules isn’t quite polished yet.

Ah, that would explain it. I guess I’ll hold off on integrating IronRuby  
just yet.

–

Thank you both for your time.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [January 15, 2011, 7:11pm UTC](https://www.ruby-forum.com/t/accessing-a-modules-classes-from-c-with-dynamic/201755/5 "2011-01-15T19:11:48Z")

</div>

Another option would be

RbScriptApp.const\_get(“Foo”)

I haven’t tried that, but I think it should work. Neither solution is  
indeed ideal. CLR interop with Modules isn’t quite polished yet.

Tomas

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [February 24, 2011, 3:41pm UTC](https://www.ruby-forum.com/t/accessing-a-modules-classes-from-c-with-dynamic/201755/6 "2011-02-24T15:41:28Z")

</div>

to add directories to your search path from within C# you can do  
something like this:

var engine = Ruby.CreateEngine();  
var searchPaths = engine.GetSearchPaths().ToList();  
searchPaths.Add(@“c:\code\generator\lib”);  
searchPaths.Add(@“C:\Ruby-ri-192\lib\ruby\1.9.1”);  
engine.SetSearchPaths(searchPaths);

Let me know if you find out how to access classes that are within  
modules from C#. I’m having no luck.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [February 25, 2011, 7:43am UTC](https://www.ruby-forum.com/t/accessing-a-modules-classes-from-c-with-dynamic/201755/7 "2011-02-25T07:43:40Z")

</div>

I got it working using a kind of a hack/workaround.

I know this is kind of a hack/workaround, but I managed to do it this  
way:

I added the next code to the end of the ruby file:

```
def hack(s)
  eval(s)
end

```

And then used it from the C# code to get the class object:

```
var engine = Ruby.CreateEngine();

var scope =

```

engine.ExecuteFile(@“c:\code\generator\lib\generator\generator\_cmd\_line.rb”);

```
var genCmdLineObj = engine.Execute(String.Format("hack('{0}::{1}')",

```

“Generator”, “CmdLine”), scope);

```
var cmdLineObj = engine.Operations.CreateInstance(genCmdLineObj);
var results = engine.Operations.InvokeMember(cmdLineObj, "run");
return Content(results);

```

Kind of a hack, but hey, it works! 🙂

## Shay.

Shay F. | Co-Founder @ CodeValue [http://codevalue.net/](http://codevalue.net/) |  
C#/IronRuby MVP | Author of IronRuby Unleashed  
Blog: [http://IronShay.com](http://IronShay.com) [http://ironshay.com/](http://ironshay.com/) | Twitter:  
[http://twitter.com/ironshay](http://twitter.com/ironshay)

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [February 25, 2011, 9:38am UTC](https://www.ruby-forum.com/t/accessing-a-modules-classes-from-c-with-dynamic/201755/8 "2011-02-25T09:38:59Z")

</div>

Wouldn’t this work?

dynamic globalConstants = engine.Runtime.Globals

globalConstants.MyModule.const\_get(‘MyClass’)

assuming

module MyModule  
class MyClass  
end  
end

Haven’t tried. Let me know if it works.

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)] On Behalf Of Shay F.  
Sent: Thursday, February 24, 2011 10:38 PM  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Ironruby-core] Accessing a Module’s Classes from C# with  
dynamic

I got it working using a kind of a hack/workaround.

I know this is kind of a hack/workaround, but I managed to do it this  
way:

I added the next code to the end of the ruby file:

```
def hack(s)
  eval(s)
end

```

And then used it from the C# code to get the class object:

```
var engine = Ruby.CreateEngine();

var scope = 

```

engine.ExecuteFile(@“c:\code\generator\lib\generator\generator\_cmd\_line.rb”);

```
var genCmdLineObj = engine.Execute(String.Format("hack('{0}::{1}')", 

```

“Generator”, “CmdLine”), scope);

```
var cmdLineObj = engine.Operations.CreateInstance(genCmdLineObj);
var results = engine.Operations.InvokeMember(cmdLineObj, "run");
return Content(results);

```

Kind of a hack, but hey, it works! 🙂

## Shay.

Shay F. | Co-Founder @ CodeValue[http://codevalue.net/](http://codevalue.net/) |  
C#/IronRuby MVP | Author of IronRuby Unleashed  
Blog: [http://IronShay.com](http://IronShay.com)[http://ironshay.com/](http://ironshay.com/) | Twitter:  
[http://twitter.com/ironshay](http://twitter.com/ironshay)

On Thu, Feb 24, 2011 at 4:41 PM, Rob R.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
to add directories to your search path from within C# you can do  
something like this:

var engine = Ruby.CreateEngine();  
var searchPaths = engine.GetSearchPaths().ToList();  
searchPaths.Add(@“c:\code\generator\lib”);  
searchPaths.Add(@“C:\Ruby-ri-192\lib\ruby\1.9.1”);  
engine.SetSearchPaths(searchPaths);

Let me know if you find out how to access classes that are within  
modules from C#. I’m having no luck.

–  
Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [February 25, 2011, 1:12pm UTC](https://www.ruby-forum.com/t/accessing-a-modules-classes-from-c-with-dynamic/201755/9 "2011-02-25T13:12:16Z")

</div>

const\_get doesn’t work, but you can do  
globalConstants.MyModule.constants()[0] but then you get back an object  
which you can’t initialize…

Shay.

On Fri, Feb 25, 2011 at 10:25 AM, Tomas M. \<
