Continuing my experiments in IronRuby, I found that
return "hi" in Ruby returned an instance of something called
MutableString, which was not easily interchangable with C#’s string:
dynamic irString = Ruby.CreateEngine().Execute("return 'hi');
Console.WriteKine(irString.GetType().Name); // Prints out "MutableString"
Assert.True(irString == "hi"); // fails?!
This caused me much confusion and befuddlement. I can’t compare for equality, nor can I call
Contains to see if
MutableString contains the substring I want to check.
The solution, trivially, was to call
ConvertToString(); this returns a plain, vanilla C#
string object (since IronRuby is implemented in C#. This works:
dynamic irString = Ruby.CreateEngine().Execute("return 'moo!').ConvertToString();
Assert.True(irString == "moo!");
For reference, I used this set of DLR/Ruby documentation to find this method.