# Forgetting MutableString

**URL:** <https://www.ruby-forum.com/t/forgetting-mutablestring/137043>\
**Category:** IronRuby\
**Created:** [May 8, 2008, 2:48pm UTC](https://www.ruby-forum.com/t/forgetting-mutablestring/137043 "2008-05-08T14:48:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![editkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/977dab/32.png) [@editkid](https://www.ruby-forum.com/u/editkid)\
**Post date:** [May 8, 2008, 2:48pm UTC](https://www.ruby-forum.com/t/forgetting-mutablestring/137043/1 "2008-05-08T14:48:15Z")

</div>

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 \<\>  
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 \<\> 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

---

<div class="post-metadata">

**Author:** ![editkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/977dab/32.png) [@editkid](https://www.ruby-forum.com/u/editkid)\
**Post date:** [May 8, 2008, 2:56pm UTC](https://www.ruby-forum.com/t/forgetting-mutablestring/137043/2 "2008-05-08T14:56:34Z")

</div>

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 B.  
Software architect  
Napa Ltd

* * *

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Peter Bacon  
Darwin  
Sent: 8. toukokuuta 2008 15:47  
To: removed\_email\_address@domain.invalid  
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.

---

<div class="post-metadata">

**Author:** ![editkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/977dab/32.png) [@editkid](https://www.ruby-forum.com/u/editkid)\
**Post date:** [May 8, 2008, 3:07pm UTC](https://www.ruby-forum.com/t/forgetting-mutablestring/137043/3 "2008-05-08T15:07:09Z")

</div>

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](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 B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![editkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/977dab/32.png) [@editkid](https://www.ruby-forum.com/u/editkid)\
**Post date:** [May 8, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/forgetting-mutablestring/137043/4 "2008-05-08T17:42:10Z")

</div>

Peter Bacon D.:

> 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

---

<div class="post-metadata">

**Author:** ![editkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/977dab/32.png) [@editkid](https://www.ruby-forum.com/u/editkid)\
**Post date:** [May 8, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/forgetting-mutablestring/137043/5 "2008-05-08T17:43:56Z")

</div>

Robert B.:

> 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

Yes - your to\_s hacks should go away 🙂

Thanks,  
-John

---

<div class="post-metadata">

**Author:** ![editkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/977dab/32.png) [@editkid](https://www.ruby-forum.com/u/editkid)\
**Post date:** [May 8, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/forgetting-mutablestring/137043/6 "2008-05-08T15:31:00Z")

</div>

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: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Steve E.  
Sent: Thursday,08 May 08, 2008 14:06  
To: removed\_email\_address@domain.invalid  
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)  
\<[http://rubyforge.org/tracker/?func=detail&aid=19873&group\_id=4359&atid=1679](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 B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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 B.

Software architect  
Napa Ltd

* * *

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Peter Bacon  
Darwin  
Sent: 8. toukokuuta 2008 15:47  
To: removed\_email\_address@domain.invalid  
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.

---

<div class="post-metadata">

**Author:** ![editkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/977dab/32.png) [@editkid](https://www.ruby-forum.com/u/editkid)\
**Post date:** [May 9, 2008, 7:41am UTC](https://www.ruby-forum.com/t/forgetting-mutablestring/137043/7 "2008-05-09T07:41:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![editkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/977dab/32.png) [@editkid](https://www.ruby-forum.com/u/editkid)\
**Post date:** [May 8, 2008, 6:31pm UTC](https://www.ruby-forum.com/t/forgetting-mutablestring/137043/8 "2008-05-08T18:31:44Z")

</div>

Peter Bacon D.:

> 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
