Converting IronRuby’s MutableString to C#’s String

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.

About Ashiq Alibhai, PMP

Ashiq has been coding C# since 2005. A desktop, web, and RIA application developer, he's touched ASP.NET MVC, ActiveRecord, Silverlight, NUnit, and all kinds of exciting .NET technologies. He started C# City in order to accelerate his .NET learning.
This entry was posted in Libraries and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *