[MacRuby-devel] Array and NSMutableArray mutability

Mark Rada mrada at marketcircle.com
Sun Jan 30 17:55:05 PST 2011


Hey,

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
	}
		

This works fine for arrays that are being made in the MacRuby VM, but if I get an array back from an Objective-C method and try to iterate over that array then I  would get an exception (which segfaulted the VM) when I tried to mutate the array during iteration. Simplified, it would be something like this:

	numbers = NSMutableArray.alloc.init
	numbers.addObjectsFromArray [1, 2, 3, 4]
	numbers.each { |number|
		numbers << (1+number) unless number > 3
		puts number
	}


Should both types of arrays work the same way? An instance of NSMutableArray says its class is Array, so I thought that they would.

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?



Mark Rada
mrada at marketcircle.com





More information about the MacRuby-devel mailing list