[Rubycocoa-devel 1153] Re: Duck typing for NSArray and NSDictionary

Back to archive index

Laurent Sansonetti lsans****@apple*****
Sun Aug 26 20:19:53 JST 2007


Hi Satoshi-san,

This is very cool, I always wanted to do that :-)

I am working on a RubyCocoa app and found that using the duck typing  
of NSString was causing some performance problem. The context was a  
method that took an NSString, and then I looped lots of time to call  
methods on the NSString. It was a lot faster to call #to_s and call  
the String methods on the instance, rather than pass via  
#method_missing. I suppose that the NSArray and NSDictionary changes  
you added will also have this problem, since they also use  
#method_missing.

One thing we could do is to automatically define the missing method in  
#method_missing. So that the next time, this will be a direct call.

# This is what we currently do when messaging Ruby -> ObjC.

What do you think?

Laurent

On Aug 26, 2007, at 4:14 AM, Satoshi Nakagawa wrote:

> I have added duck typing for NSArray and NSDictionary in r2009.
> Now NSArray and NSDictionary behave Array and Hash respectively.
>
>    a = NSMutableArray.alloc.init
>    a << 1 << 2 << 3
>    p a.map {|i| i.to_i*3 }
>    #=> [3, 6, 9]
>
>    dic = NSMutableDictionary.alloc.init
>    dic[1] = 'one'
>    dic[2] = 'two'
>    h= { 3 => 'three', 4 => 'four'}
>    dic.merge!(h)
>    dic.each {|k,v| puts "#{k.to_i}: #{v.to_s}" }
>    #=>
>    1: one
>    2: two
>    3: three
>    4: four
>
> --
> Satoshi Nakagawa
>
> _______________________________________________
> Rubycocoa-devel mailing list
> Rubyc****@lists*****
> http://lists.sourceforge.jp/mailman/listinfo/rubycocoa-devel




More information about the Rubycocoa-devel mailing list
Back to archive index