1

I'm playing around with IronRuby. I made the stubs of an interactive fiction game. In C#, I have a Command class which represents a command (name, text to type, and action delegate):

public delegate string CommandAction(string target, string instrument, string preposition); private CommandAction action;

    public Command(string name, string[] verbs, CommandAction action)
    {
        this.Name = name;
        this.Verbs = verbs;
        this.action = action;
    }

I create commands in C# without any problem. Here's the simplest:

this.knownCommands.Add(new Command("Quit", new string[] { "q", "quit" }, (t, i, p) =>
{
    isRunning = false;
    return "Bye!";
}));

(Don't worry about the three parameters right now, they're all strings.)

I want to make a command and add it to my IEnumerable<Command> list. Here's the Ruby code:

def to_clr_string_array(list)
    System::Array[System::String].new(list.map { |s| s.to_s.to_clr_string })
end

require 'Meltdown.Core.dll'
include Meltdown::Core

Command.new("Ruby Command", to_clr_string_array(['rb']), Proc.new { |target, preposition, instrument|
    puts "Command invoked with #{target}, #{instrument}, and #{preposition}"
})

(to_clr_string_array is necessary to convert list types.) When I try to instantiate this, I get a type conversion error: Can't Convert Meltdown::Core::Command into Meltdown::Core::Command. Here's how I do it:

var engine = Ruby.CreateEngine();
var scope = Engine.Runtime.CreateScope();
string contents = System.IO.File.ReadAllText(scriptPath);
var command = engine.Execute<Command>(contents, scope);

The third line fails. I tried this answer to a similar problem, coupled with changing my array into a list of objects. Instead of the instantiation failing, when I execute the command, I get an error that the types are different.

In that detailed error, it mentions exactly the same error as the question in the answer I linked: the types differ only in their context (Default and LoadNeither).

Unfortunately, that doesn't help me, and I still can't figure out how to do this. I've tried also passing my list of commands into Ruby, but I get an error that the types don't match and it can't go into my list.

What am I doing wrong?

1 Answer 1

1

I posted this question to the ironruby-core mailing list. Brandon Doot replied and suggested I add:

load_assembly 'SharedClasses'

This solved the problem. The types are now the same as far as .NET is concerned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.