[Ironruby-core] Accessing a Module's Classes from C# with dynamic

Joshua M. lists at ruby-forum.com
Thu Jan 13 15:51:48 EST 2011

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();

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

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 EnumerateConstants and manual
lookup of the class name I want, but it doesn't have the syntactic
elegance of the dynamic keyword... Any thoughts?


Posted via http://www.ruby-forum.com/.

More information about the Ironruby-core mailing list