[Ironruby-core] Comparing CLR strings and Ruby strings - a slightly surprising behaviour
curth at microsoft.com
Tue Mar 3 17:41:54 EST 2009
This mismatch doesn't exist in Python because Python's string semantics are largely compatible with .NET's string semantics. As a result, Python can actually use .NET's strings as Python strings. Ruby strings, unfortunately, are mutable, which means that IronRuby has to use a different type to store a mutable string. In many cases, the binder should automatically perform the conversion between a CLR string and a mutable string. There are probably places (like this one) where something just hasn't been implemented yet. And there may be places where we simply can't do an automatic conversion.
The mutable string type in Ruby is (in my opinion) one of the most unfortunate design decisions made in the language.
From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Meinrad Recheis
Sent: Tuesday, March 03, 2009 2:19 PM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] Comparing CLR strings and Ruby strings - a slightly surprising behaviour
On Tue, Mar 3, 2009 at 11:11 PM, Meinrad Recheis <meinrad.recheis at gmail.com<mailto:meinrad.recheis at gmail.com>> wrote:
On Tue, Mar 3, 2009 at 9:46 PM, Thibaut Barrère <thibaut.barrere at gmail.com<mailto:thibaut.barrere at gmail.com>> wrote:
while writing specs for Magic, I noticed that:
instance_from(MenuItem, "Hello").text.to_s.should == "Hello"
to_s is required to get the assertion to pass. The following will return false:
button = Button.new
button.text = "Hello"
puts button.text == "Hello"
So I guess that CLR strings cannot be compared to Ruby strings unless to_s is applied.
I have stumbled on this too and was very surprised. This problem does not exist in IronPython (does it? please correct me if I am wrong). The behavior I was expecting is that "on the ruby" side all strings are (or at least behave exactly like) ruby strings, even if they come from .NET and are automatically converted to clr-strings when passed into clr methods.
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...
More information about the Ironruby-core