Brian Marick
maric****@examp*****
Fri Apr 24 06:44:42 JST 2009
Suppose you have an NSArray containing some Objective-C objects. I understand why the following can be false: array[0] == array[0] Each time the element is extracted from the array, it could get a new "ruby half". The main point of this note is to confirm that's correct behavior. The lesser part of the note: I don't understand why the behavior isn't consistent. Consider this program: #! /usr/bin/env ruby require 'osx/cocoa' include OSX def run(klass, count) array = make_array(klass, count) first = collect_the_object_ids(array) second = collect_the_object_ids(array) if first == second puts "success" else puts "FAILURE for #{klass.name}" puts first[0,10].inspect puts second[0,10].inspect end end def make_array(klass, count) (0...count).collect { klass.alloc.init }.to_ns end def collect_the_object_ids(array) array.collect { | e | e.object_id }.to_ruby end run(NSDictionary, 1) run(NSString, 1) run(NSTextField, 100000) It consistently produces these results: FAILURE for OSX::NSDictionary [3079050] [3078130] FAILURE for OSX::NSString [3077490] [3077260] success An NSManagedObject in a 16 element NSArray had a stable object_id the first 17 times it was extracted, then it changed. There's caching going on in some cases. What are the rules behind them? Thanks. ----- Brian Marick, independent consultant Mostly on agile methods with a testing slant www.exampler.com, www.exampler.com/blog, www.twitter.com/marick