[Ironruby-core] Comparing CLR strings and Ruby strings - a slightly surprising behaviour

Jimmy Schementi Jimmy.Schementi at microsoft.com
Wed Mar 4 01:08:59 EST 2009

(This is my understanding of the problem, so Tomas please correct me if it's wrong ... I wasn't involved in the initial decision).

The "mutableness" of a string is the defining distinction, so we don't make CLR string act like a Ruby string in cases, because it won't allow mutation. This is the same reason we can't easily allow Ruby methods to operate on CLR strings, because mutating methods won't work (chomp!, etc). So rather than only supporting part of the Ruby methods on CLR strings, you have to explicitly ask for one or the other.

In practice, I haven't felt much pain by this when hosting IronRuby. The hosting layer can make sure to convert any CLR string into a mutable string, and any strings pulled out of IronRuby code can be turned into CLR strings by the way Tomas showed below.


From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Meinrad Recheis
Sent: Tuesday, March 03, 2009 4:25 PM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] Comparing CLR strings and Ruby strings - a slightly surprising behaviour

Ok, the Execute<T> is convenient enough. In my opinion the highlighted C# code represents a potential interoperability pitfall and will need to be documented well.
On the ruby side, I think there are no technical limitations to achieve ruby's string behavior even if the underlying object is an immutable clr string. What do you think?
-- henon
On Wed, Mar 4, 2009 at 12:24 AM, Tomas Matousek <Tomas.Matousek at microsoft.com<mailto:Tomas.Matousek at microsoft.com>> wrote:

We don't plan to make the highlighted code work. However we are going to make this work:

string s = engine.Execute<string>("'hi'")

and also this works:

string s = engine.Execute("'hi'").ToString(),

although in this case you need to take care of null.

The difference is that unlike Execute, Execute<T> invokes an explicit dynamic conversion on the resulting type. You can achieve the same using engine.ObjectOperations.ConvertTo<string>(engine.Execute("'hi'")).



In addition to that, it would also be great to have automatic type conversion on the .NET side too. I'd expect to be able to assign a string pulled out from the interpreter to a C# string without the need to call ToString(). For example,

string s = engine.Execute("'hi'") as string;

Currently s would be null because the dynamic cast to System.String fails.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20090303/892ec578/attachment.html>

More information about the Ironruby-core mailing list