[MacRuby-devel] Array and NSMutableArray mutability

Vincent Isambart vincent.isambart at gmail.com
Sun Jan 30 21:39:35 PST 2011


Hi,

> I was trying to implement a breadth first search where I would mutate the search queue while iterating through it. Simplified, it was something like this:
>
>        numbers = [1, 2, 3, 4]
>        numbers.each { |number|
>                numbers << (1+number) unless number > 3
>                puts number
>        }

Modifying the object you're iterating on is undefined behavior, even in Ruby:
http://www.ruby-forum.com/topic/188211#821599

It's generally easy to modify your code to either iterate on a copy of
the array or looping with an index (numbers.length.times {...} or i =
0; while i < numbers.length; ..; end depending on what you want to
do).

> Also, is this type of exception one that should be caught by the VM and propagated as a normal Ruby Exception that I could rescue?

It should definitively be propagated as a normal Ruby exception. Could
you file a ticket on trac for that?


More information about the MacRuby-devel mailing list