I keep forgetting to wrap up CLR strings in MutableStrings when returning strings retrieved from calls to .NET Framework library methods. I had this RSpec that had something like: some_array[3].should == "some mutable string" that was failing with the error: Expected "some mutable string" to equal "some mutable string": It would be great if there were some way of picking this up, possibly at compile time (possibly with SpecSharp??) or may be more simply changing the way a CLR string is printed in the console so that it is different from a MutableString. For instance how about a CLR string being printed out as <<my CLR string>> or <CLR String "my CLR string"> or something. This could be a debug thing only if you wanted. Then the error message would be: Expected <<some mutable string>> to equal "some mutable string" Which makes it more clear what is wrong, even if you don''t know the convention. [I realise that I ought to just check that the class of the some_array[3] was actually String in the first place but this would still help.] [Also, I realise that there is no implicit conversion between CLR string and MutableString, so if the method was typed to return a MutableString then this would not compile, but Ruby doesn''t half like having stuff returned in arrays, so you lose the strong typing] Pete -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20080508/40b13d6c/attachment.html>
I would guess that IR-teams plan is to implement auto-wrapping of strings coming from DotNet as MutableStrings? At least I hope so since that would get rid of my code of extensive to_s calls from parameters / values coming from C# :-) Robert Brotherus Software architect Napa Ltd _____ From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Peter Bacon Darwin Sent: 8. toukokuuta 2008 15:47 To: ironruby-core at rubyforge.org Subject: [Ironruby-core] Forgetting MutableString I keep forgetting to wrap up CLR strings in MutableStrings when returning strings retrieved from calls to .NET Framework library methods. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20080508/c040e6eb/attachment.html>
I wasn''t a fan of all the calls to to_s either so I put together a small patch (which undoubtedly is nothing like what the actual fix is going to be) to relieve my pains. The bug I submitted along with a patch I made locally can be found at: http://rubyforge.org/tracker/?func=detail&aid=19873&group_id=4359&atid=16798 Cheers, Steve On Thu, May 8, 2008 at 8:55 AM, Robert Brotherus <robert.brotherus at napa.fi> wrote:> I would guess that IR-teams plan is to implement auto-wrapping of strings > coming from DotNet as MutableStrings? > > At least I hope so since that would get rid of my code of extensive *to_s*calls from parameters / values coming from C# :-) > * > > Robert Brotherus > *Software architect > *Napa Ltd > * > > ------------------------------ > *From:* ironruby-core-bounces at rubyforge.org [mailto: > ironruby-core-bounces at rubyforge.org] *On Behalf Of *Peter Bacon Darwin > *Sent:* 8. toukokuuta 2008 15:47 > *To:* ironruby-core at rubyforge.org > *Subject:* [Ironruby-core] Forgetting MutableString > > I keep forgetting to wrap up CLR strings in MutableStrings when returning > strings retrieved from calls to .NET Framework library methods. > > > > > > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20080508/957d5e37/attachment.html>
The trouble is that MutableStrings and CLR Strings are (going to be, if not already) quite different creatures. For a start the CLR string is always UTF-16, while the MutableString is basically just an array of bytes, which can be interpreted in different ways depending on the K-Code you are using. In order to compare these two kinds of strings you need an intelligent comparer that understands what locale you are working against. I understand that Tomas is working on a version of MutableString that has a hybrid internal organisation to enable easy cooperation with CLR strings while maintaining compatibility with Ruby strings (hopefully without losing out in performance). Pete From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Steve Eichert Sent: Thursday,08 May 08, 2008 14:06 To: ironruby-core at rubyforge.org Subject: Re: [Ironruby-core] Forgetting MutableString I wasn''t a fan of all the calls to to_s either so I put together a small patch (which undoubtedly is nothing like what the actual fix is going to be) to relieve my pains. The bug I submitted along with a patch I made locally can be found at: http://rubyforge.org/tracker/?func=detail <http://rubyforge.org/tracker/?func=detail&aid=19873&group_id=4359&atid=1679 8> &aid=19873&group_id=4359&atid=16798 Cheers, Steve On Thu, May 8, 2008 at 8:55 AM, Robert Brotherus <robert.brotherus at napa.fi> wrote: I would guess that IR-teams plan is to implement auto-wrapping of strings coming from DotNet as MutableStrings? At least I hope so since that would get rid of my code of extensive to_s calls from parameters / values coming from C# :-) Robert Brotherus Software architect Napa Ltd _____ From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Peter Bacon Darwin Sent: 8. toukokuuta 2008 15:47 To: ironruby-core at rubyforge.org Subject: [Ironruby-core] Forgetting MutableString I keep forgetting to wrap up CLR strings in MutableStrings when returning strings retrieved from calls to .NET Framework library methods. _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20080508/47f9952d/attachment-0001.html>
Peter Bacon Darwin:> I understand that Tomas is working on a version of MutableString that > has a hybrid internal organisation to enable easy cooperation with CLR > strings while maintaining compatibility with Ruby strings (hopefully > without losing out in performance).Yes- Tomas is working on a new MutableString this week. The idea is to avoid perf hits in the general case (eg a byte string is created and you are doing byte-string like things to it). However we will lazy eval conversions to other string types where necessary. Not sure where things will line up when the new implementation shows up this week or next. Thanks, -John
Robert Brotherus:> At least I hope so since that would get rid of my code of extensive > to_s calls from parameters / values coming from C# :-) Robert > Brotherus Software architect Napa LtdYes - your to_s hacks should go away :) Thanks, -John
Peter Bacon Darwin:> I keep forgetting to wrap up CLR strings in MutableStrings when > returning strings retrieved from calls to .NET Framework library > methods.I finally understand what this is ... is this what you''re looking for? public override bool Equals(object other) { if (other is MutableString) return Equals(other as MutableString); else if (other is string) return Equals(other as string); else return false; } public bool Equals(string other) { if (other == null) return false; if (_data.Length != other.Length) return false; for (int i = 0; i < _data.Length; ++i) if (_data[i] != other[i]) return false; return true; } public bool Equals(MutableString other) { if (ReferenceEquals(this, other)) return true; if (other == null) return false; if (_data.Length != other.Length) return false; for (int i = 0; i < _data.Length; ++i) if (_data[i] != other.Chars[i]) return false; return true; } If so, it''s coming in the next release. Thanks, -John
This actually makes my problem worse! Now there is no way of catching the times when I accidentally return a CLR string rather than a MutableString. It will only flag up when client code tries to call a MutableString method on the CLR string. Pete -----Original Message----- From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of John Lam (IRONRUBY) Sent: Thursday,08 May 08, 2008 17:23 To: ironruby-core at rubyforge.org Subject: Re: [Ironruby-core] Forgetting MutableString Peter Bacon Darwin:> I keep forgetting to wrap up CLR strings in MutableStrings when > returning strings retrieved from calls to .NET Framework library > methods.I finally understand what this is ... is this what you''re looking for? public override bool Equals(object other) { if (other is MutableString) return Equals(other as MutableString); else if (other is string) return Equals(other as string); else return false; } public bool Equals(string other) { if (other == null) return false; if (_data.Length != other.Length) return false; for (int i = 0; i < _data.Length; ++i) if (_data[i] != other[i]) return false; return true; } public bool Equals(MutableString other) { if (ReferenceEquals(this, other)) return true; if (other == null) return false; if (_data.Length != other.Length) return false; for (int i = 0; i < _data.Length; ++i) if (_data[i] != other.Chars[i]) return false; return true; } If so, it''s coming in the next release. Thanks, -John _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core