[MacRuby-devel] new Hash class

Laurent Sansonetti lsansonetti at apple.com
Wed Mar 19 14:50:46 PDT 2008


Hi,

If you have been following trunk very recently you probably know. The  
Hash class now has a new implementation. So far I'm happy with the  
changes that are in trunk, and here are the details.

- The Hash class is still a subclass of NSMutableDictionary.

- All the Hash methods are not defined on Hash anymore, but on  
NSDictionary instead. The methods have been re-implemented to use the  
CF APIs. It means that you can for example call #has_key? on all  
dictionaries, created by Ruby (via the Hash class) or by Objective-C  
(as a NSCFDictionary object). Trying to call a method that will change  
the dictionary on an immutable dictionary will raise an exception  
("can't modify immutable hash").

- To implement the semantics of the previous Hash class (such as the  
possibility to freeze it, or specify an "if none" value/proc), MacRuby  
allocates at demand a data structure and uses a feature of the  
Objective-C GC to associate/attach it to the dictionary. Once the  
dictionary is collected, the data structure is also collected at the  
same time (w00t!).

So far, with these changes, MacRuby builds (yeepee!) and most of the  
tests in test/ruby/test_hash.rb are passing.

Things that are not working yet:

- Tainting objects. We need to attach the taint flag in the attached  
structure (like we do with the frozen state).

- Passing a false value. Qfalse is 0 in Ruby core, and apparently  
CFDictionary (via CFDictionaryGetValueIfPresent) has some problems to  
remove it (though lookups are working). This should anyway be  
addressed once "false" is something different than 0 in the core.

- Hash#compare_by_identity. Supporting that wouldn't be hard by  
calling some private APIs in CFDictionary.

We can now start replacing Array. I proposed on ruby-core to deprecate  
RARRAY_PTR for element access, which we won't be able to efficiently  
support. Nobu-san proposed RARRAY_AT() which should work for us.

Also, Ben and I have been brainstorming on Monday about future  
changes, and here is what we decided:

http://trac.macosforge.org/projects/ruby/wiki/MacRubyBrainstorming

  Laurent


More information about the MacRuby-devel mailing list