# MutableString.each

**URL:** <https://www.ruby-forum.com/t/mutablestring-each/141715>\
**Category:** IronRuby\
**Created:** [June 30, 2008, 12:59am UTC](https://www.ruby-forum.com/t/mutablestring-each/141715 "2008-06-30T00:59:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_Remy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@Dave\_Remy](https://www.ruby-forum.com/u/Dave_Remy)\
**Post date:** [June 30, 2008, 12:59am UTC](https://www.ruby-forum.com/t/mutablestring-each/141715/1 "2008-06-30T00:59:14Z")

</div>

I am working on one of the specs for string.each which is failing.  
This is the spec that is failing.

it “raises a RuntimeError if the string is modified while substituting”  
do  
str = “hello\nworld”  
should\_raise(RuntimeError) { str.send(cmd) { str[0] = ‘x’ } }  
end

The idea is that the string that is being iterated over shouldn’t be  
changed during the iteration. It is easy enough to freeze the string in  
the first line of MutableString.EachLine using:  
RubyUtils.GetExecutionContext(context).FreezeObject(self); Which  
appropriately throws an error when the string gets modified, however  
this approach throws a “TypeError” and the spec wants a “RuntimeError”  
(message: String#each raises a RuntimeError if the string is modified  
while substituting FAILED, Expected RuntimeError, got TypeError (can’t  
modify frozen object)). I considered wrapping the code in EachLine  
that invokes the each block with a try catch and then rethrowing a  
caught type error exception to a runtime exception however I don’t see a  
runtime exception type in RubyExceptions. Any suggestions on how best  
to approach this?

Thx!  
rem

---

<div class="post-metadata">

**Author:** ![Dave\_Remy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@Dave\_Remy](https://www.ruby-forum.com/u/Dave_Remy)\
**Post date:** [June 30, 2008, 1:16am UTC](https://www.ruby-forum.com/t/mutablestring-each/141715/2 "2008-06-30T01:16:47Z")

</div>

RuntimeException is defined in  
IronRuby.Libraries\Builtins\Exceptions.cs.

Is MRI really that inconsistent about which type of error is thrown when  
you try to modify a frozen object? (Not that this would surprise me :(.)  
If so, it might be cleaner if the call to FreezeObject could record or  
otherwise influence the type of exception that we expect to throw.

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Dave Remy  
Sent: Sunday, June 29, 2008 3:58 PM  
To: removed\_email\_address@domain.invalid  
Subject: [Ironruby-core] MutableString.each

I am working on one of the specs for string.each which is failing.  
This is the spec that is failing.

it “raises a RuntimeError if the string is modified while substituting”  
do  
str = “hello\nworld”  
should\_raise(RuntimeError) { str.send(cmd) { str[0] = ‘x’ } }  
end

The idea is that the string that is being iterated over shouldn’t be  
changed during the iteration. It is easy enough to freeze the string in  
the first line of MutableString.EachLine using:  
RubyUtils.GetExecutionContext(context).FreezeObject(self); Which  
appropriately throws an error when the string gets modified, however  
this approach throws a “TypeError” and the spec wants a “RuntimeError”  
(message: String#each raises a RuntimeError if the string is modified  
while substituting FAILED, Expected RuntimeError, got TypeError (can’t  
modify frozen object)). I considered wrapping the code in EachLine  
that invokes the each block with a try catch and then rethrowing a  
caught type error exception to a runtime exception however I don’t see a  
runtime exception type in RubyExceptions. Any suggestions on how best  
to approach this?

Thx!  
rem

---

<div class="post-metadata">

**Author:** ![Dave\_Remy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@Dave\_Remy](https://www.ruby-forum.com/u/Dave_Remy)\
**Post date:** [June 30, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/mutablestring-each/141715/3 "2008-06-30T18:47:39Z")

</div>

Translation: I need to check to assure that mri actually uses freeze to  
make the string immutable while iterating. I should be able to test the  
string for frozen. I just ran a test script, and nope, the string  
being iterated is not frozen:  
s = “aaa”  
s.each { puts s.frozen? } =\> false

So I need to implement some other mechanism for making the iterated  
string immutable during the iteration rather than freezing it. I  
wonder whether other types of mutable objects that iterate have this  
same behavior … I’ll do some more research.

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of John L.  
(IRONRUBY)  
Sent: Monday, June 30, 2008 7:23 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] MutableString.each

You should catch and rethrow the correct exception if you want to use  
the frozen detection stuff. But you should check to see if it’s actually  
frozen while iterating - ie is this an observable side-effect of using  
each?

Thanks,  
-John

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Curt  
Hagenlocher  
Sent: Sunday, June 29, 2008 4:16 PM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] MutableString.each

RuntimeException is defined in  
IronRuby.Libraries\Builtins\Exceptions.cs.

Is MRI really that inconsistent about which type of error is thrown when  
you try to modify a frozen object? (Not that this would surprise me :(.)  
If so, it might be cleaner if the call to FreezeObject could record or  
otherwise influence the type of exception that we expect to throw.

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Dave Remy  
Sent: Sunday, June 29, 2008 3:58 PM  
To: removed\_email\_address@domain.invalid  
Subject: [Ironruby-core] MutableString.each

I am working on one of the specs for string.each which is failing.  
This is the spec that is failing.

it “raises a RuntimeError if the string is modified while substituting”  
do  
str = “hello\nworld”  
should\_raise(RuntimeError) { str.send(cmd) { str[0] = ‘x’ } }  
end

The idea is that the string that is being iterated over shouldn’t be  
changed during the iteration. It is easy enough to freeze the string in  
the first line of MutableString.EachLine using:  
RubyUtils.GetExecutionContext(context).FreezeObject(self); Which  
appropriately throws an error when the string gets modified, however  
this approach throws a “TypeError” and the spec wants a “RuntimeError”  
(message: String#each raises a RuntimeError if the string is modified  
while substituting FAILED, Expected RuntimeError, got TypeError (can’t  
modify frozen object)). I considered wrapping the code in EachLine  
that invokes the each block with a try catch and then rethrowing a  
caught type error exception to a runtime exception however I don’t see a  
runtime exception type in RubyExceptions. Any suggestions on how best  
to approach this?

Thx!  
rem

---

<div class="post-metadata">

**Author:** ![Dave\_Remy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@Dave\_Remy](https://www.ruby-forum.com/u/Dave_Remy)\
**Post date:** [June 30, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/mutablestring-each/141715/4 "2008-06-30T16:23:56Z")

</div>

You should catch and rethrow the correct exception if you want to use  
the frozen detection stuff. But you should check to see if it’s actually  
frozen while iterating - ie is this an observable side-effect of using  
each?

Thanks,  
-John

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Curt  
Hagenlocher  
Sent: Sunday, June 29, 2008 4:16 PM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] MutableString.each

RuntimeException is defined in  
IronRuby.Libraries\Builtins\Exceptions.cs.

Is MRI really that inconsistent about which type of error is thrown when  
you try to modify a frozen object? (Not that this would surprise me :(.)  
If so, it might be cleaner if the call to FreezeObject could record or  
otherwise influence the type of exception that we expect to throw.

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Dave Remy  
Sent: Sunday, June 29, 2008 3:58 PM  
To: removed\_email\_address@domain.invalid  
Subject: [Ironruby-core] MutableString.each

I am working on one of the specs for string.each which is failing.  
This is the spec that is failing.

it “raises a RuntimeError if the string is modified while substituting”  
do  
str = “hello\nworld”  
should\_raise(RuntimeError) { str.send(cmd) { str[0] = ‘x’ } }  
end

The idea is that the string that is being iterated over shouldn’t be  
changed during the iteration. It is easy enough to freeze the string in  
the first line of MutableString.EachLine using:  
RubyUtils.GetExecutionContext(context).FreezeObject(self); Which  
appropriately throws an error when the string gets modified, however  
this approach throws a “TypeError” and the spec wants a “RuntimeError”  
(message: String#each raises a RuntimeError if the string is modified  
while substituting FAILED, Expected RuntimeError, got TypeError (can’t  
modify frozen object)). I considered wrapping the code in EachLine  
that invokes the each block with a try catch and then rethrowing a  
caught type error exception to a runtime exception however I don’t see a  
runtime exception type in RubyExceptions. Any suggestions on how best  
to approach this?

Thx!  
rem

---

<div class="post-metadata">

**Author:** ![Dave\_Remy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@Dave\_Remy](https://www.ruby-forum.com/u/Dave_Remy)\
**Post date:** [June 30, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/mutablestring-each/141715/5 "2008-06-30T22:09:46Z")

</div>

After trying out various use cases string.each (synonym for .each\_line)  
is very weird. Here are some cases …

# This one does not throw exception

s = “aaa”  
s.each { s[0] = “b”}  
=\> “baa”

# This one does (it has a newline) (and matches the rubyspec test case)

s = “aaa\naaa”  
s.each { s[0] = “b”}  
=\> Runtime Error: string modified

# Ok, if that is not weird enough (but maybe not completely whacked if

you consider each\_line won’t really iterate until there is a newline)  
s = “aaa\naaa”

# modify s before iterating

s[0] = “a”  
s.each { s[0] = “b”}  
=\> “baa\naaa”

This looks like MRI is relying on copy on write semantics to determine  
whether a string was modified? Jim, says this is not the behavior for  
1.9.

This seems like a good candidate for IR diverging on …

rem

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Dave Remy  
Sent: Monday, June 30, 2008 9:47 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] MutableString.each

Translation: I need to check to assure that mri actually uses freeze to  
make the string immutable while iterating. I should be able to test the  
string for frozen. I just ran a test script, and nope, the string  
being iterated is not frozen:  
s = “aaa”  
s.each { puts s.frozen? } =\> false

So I need to implement some other mechanism for making the iterated  
string immutable during the iteration rather than freezing it. I  
wonder whether other types of mutable objects that iterate have this  
same behavior … I’ll do some more research.

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of John L.  
(IRONRUBY)  
Sent: Monday, June 30, 2008 7:23 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] MutableString.each

You should catch and rethrow the correct exception if you want to use  
the frozen detection stuff. But you should check to see if it’s actually  
frozen while iterating - ie is this an observable side-effect of using  
each?

Thanks,  
-John

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Curt  
Hagenlocher  
Sent: Sunday, June 29, 2008 4:16 PM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] MutableString.each

RuntimeException is defined in  
IronRuby.Libraries\Builtins\Exceptions.cs.

Is MRI really that inconsistent about which type of error is thrown when  
you try to modify a frozen object? (Not that this would surprise me :(.)  
If so, it might be cleaner if the call to FreezeObject could record or  
otherwise influence the type of exception that we expect to throw.

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Dave Remy  
Sent: Sunday, June 29, 2008 3:58 PM  
To: removed\_email\_address@domain.invalid  
Subject: [Ironruby-core] MutableString.each

I am working on one of the specs for string.each which is failing.  
This is the spec that is failing.

it “raises a RuntimeError if the string is modified while substituting”  
do  
str = “hello\nworld”  
should\_raise(RuntimeError) { str.send(cmd) { str[0] = ‘x’ } }  
end

The idea is that the string that is being iterated over shouldn’t be  
changed during the iteration. It is easy enough to freeze the string in  
the first line of MutableString.EachLine using:  
RubyUtils.GetExecutionContext(context).FreezeObject(self); Which  
appropriately throws an error when the string gets modified, however  
this approach throws a “TypeError” and the spec wants a “RuntimeError”  
(message: String#each raises a RuntimeError if the string is modified  
while substituting FAILED, Expected RuntimeError, got TypeError (can’t  
modify frozen object)). I considered wrapping the code in EachLine  
that invokes the each block with a try catch and then rethrowing a  
caught type error exception to a runtime exception however I don’t see a  
runtime exception type in RubyExceptions. Any suggestions on how best  
to approach this?

Thx!  
rem

---

<div class="post-metadata">

**Author:** ![Dave\_Remy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@Dave\_Remy](https://www.ruby-forum.com/u/Dave_Remy)\
**Post date:** [June 30, 2008, 10:13pm UTC](https://www.ruby-forum.com/t/mutablestring-each/141715/6 "2008-06-30T22:13:52Z")

</div>

Note that on 1.9 you have to use each\_line since each (and enumerable)  
have been removed from the String class.

JD

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Dave Remy  
Sent: Monday, June 30, 2008 1:06 PM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] MutableString.each

After trying out various use cases string.each (synonym for .each\_line)  
is very weird. Here are some cases …

# This one does not throw exception

s = “aaa”  
s.each { s[0] = “b”}  
=\> “baa”

# This one does (it has a newline) (and matches the rubyspec test case)

s = “aaa\naaa”  
s.each { s[0] = “b”}  
=\> Runtime Error: string modified

# Ok, if that is not weird enough (but maybe not completely whacked if

you consider each\_line won’t really iterate until there is a newline)  
s = “aaa\naaa”

# modify s before iterating

s[0] = “a”  
s.each { s[0] = “b”}  
=\> “baa\naaa”

This looks like MRI is relying on copy on write semantics to determine  
whether a string was modified? Jim, says this is not the behavior for  
1.9.

This seems like a good candidate for IR diverging on …

rem

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Dave Remy  
Sent: Monday, June 30, 2008 9:47 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] MutableString.each

Translation: I need to check to assure that mri actually uses freeze to  
make the string immutable while iterating. I should be able to test the  
string for frozen. I just ran a test script, and nope, the string  
being iterated is not frozen:  
s = “aaa”  
s.each { puts s.frozen? } =\> false

So I need to implement some other mechanism for making the iterated  
string immutable during the iteration rather than freezing it. I  
wonder whether other types of mutable objects that iterate have this  
same behavior … I’ll do some more research.

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of John L.  
(IRONRUBY)  
Sent: Monday, June 30, 2008 7:23 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] MutableString.each

You should catch and rethrow the correct exception if you want to use  
the frozen detection stuff. But you should check to see if it’s actually  
frozen while iterating - ie is this an observable side-effect of using  
each?

Thanks,  
-John

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Curt  
Hagenlocher  
Sent: Sunday, June 29, 2008 4:16 PM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] MutableString.each

RuntimeException is defined in  
IronRuby.Libraries\Builtins\Exceptions.cs.

Is MRI really that inconsistent about which type of error is thrown when  
you try to modify a frozen object? (Not that this would surprise me :(.)  
If so, it might be cleaner if the call to FreezeObject could record or  
otherwise influence the type of exception that we expect to throw.

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Dave Remy  
Sent: Sunday, June 29, 2008 3:58 PM  
To: removed\_email\_address@domain.invalid  
Subject: [Ironruby-core] MutableString.each

I am working on one of the specs for string.each which is failing.  
This is the spec that is failing.

it “raises a RuntimeError if the string is modified while substituting”  
do  
str = “hello\nworld”  
should\_raise(RuntimeError) { str.send(cmd) { str[0] = ‘x’ } }  
end

The idea is that the string that is being iterated over shouldn’t be  
changed during the iteration. It is easy enough to freeze the string in  
the first line of MutableString.EachLine using:  
RubyUtils.GetExecutionContext(context).FreezeObject(self); Which  
appropriately throws an error when the string gets modified, however  
this approach throws a “TypeError” and the spec wants a “RuntimeError”  
(message: String#each raises a RuntimeError if the string is modified  
while substituting FAILED, Expected RuntimeError, got TypeError (can’t  
modify frozen object)). I considered wrapping the code in EachLine  
that invokes the each block with a try catch and then rethrowing a  
caught type error exception to a runtime exception however I don’t see a  
runtime exception type in RubyExceptions. Any suggestions on how best  
to approach this?

Thx!  
rem
