[Ironruby-core] MutableString.each

Dave Remy dremy at microsoft.com
Mon Jun 30 12:46:57 EDT 2008


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: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of John Lam (IRONRUBY)
Sent: Monday, June 30, 2008 7:23 AM
To: ironruby-core at rubyforge.org
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: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Curt Hagenlocher
Sent: Sunday, June 29, 2008 4:16 PM
To: ironruby-core at rubyforge.org
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: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Dave Remy
Sent: Sunday, June 29, 2008 3:58 PM
To: ironruby-core at rubyforge.org
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

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


More information about the Ironruby-core mailing list