NSArray's count and Ruby Array#count
Hi all, Code first: cocoa_array = NSArray.new ruby_array = [] puts ruby_array.count # => 0 puts ruby_array.count { true } # => 0 puts ruby_array.count("whatever") # => 0 puts cocoa_array.count # => 0 puts cocoa_array.count { true } # => 0 # unknown: warning: passing a block to an Objective-C method - will be ignored puts cocoa_array.count("whatever") # => wrong number of arguments (1 for 0) (ArgumentError) I originally ran into this issue in the following code, where the fact that we have an NSArray, and not a ruby array ends up concealed by the bajillion of ruby-ish method calls such as compact and map: paths_from_clipboard = NSPasteboard.generalPasteboard.pasteboardItems .map { |pbi | pbi.stringForType('public.file-url') }.compact .map { |url | NSURL.URLWithString(url).path } .map { |path| Pathname.new(path) } Full, pretty and colorful version: http://gist.github.com/602174 What's going on there is that NSArrays (but not NSMutableArrays) will use the vanilla Cocoa's count method. So I'm resorting to doing: Array.new(SomeCocoaClass.withAMethodThatReturnsAnNSArray) I wonder, is this a bug or "works as designed"?
Hi Caio, You found out an exception :) As you know, NSArray responds to -count which simply return the number of elements it contains. But Ruby Array defines #count which has different semantics. To not conflict with NSArray, the Ruby #count is only defined on Ruby arrays. However, I do not understand where #count is used in your snippet. Is it used in Pathname? Laurent On Oct 4, 2010, at 12:14 AM, Caio Chassot wrote:
Hi all,
Code first:
cocoa_array = NSArray.new ruby_array = []
puts ruby_array.count # => 0 puts ruby_array.count { true } # => 0 puts ruby_array.count("whatever") # => 0
puts cocoa_array.count # => 0 puts cocoa_array.count { true } # => 0 # unknown: warning: passing a block to an Objective-C method - will be ignored puts cocoa_array.count("whatever") # => wrong number of arguments (1 for 0) (ArgumentError)
I originally ran into this issue in the following code, where the fact that we have an NSArray, and not a ruby array ends up concealed by the bajillion of ruby-ish method calls such as compact and map:
paths_from_clipboard = NSPasteboard.generalPasteboard.pasteboardItems .map { |pbi | pbi.stringForType('public.file-url') }.compact .map { |url | NSURL.URLWithString(url).path } .map { |path| Pathname.new(path) }
Full, pretty and colorful version: http://gist.github.com/602174
What's going on there is that NSArrays (but not NSMutableArrays) will use the vanilla Cocoa's count method.
So I'm resorting to doing:
Array.new(SomeCocoaClass.withAMethodThatReturnsAnNSArray)
I wonder, is this a bug or "works as designed"?
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
On 2010-10-05, at 18:15 , Laurent Sansonetti wrote:
You found out an exception :) As you know, NSArray responds to -count which simply return the number of elements it contains. But Ruby Array defines #count which has different semantics.
To not conflict with NSArray, the Ruby #count is only defined on Ruby arrays.
Yeah. But it appears to me ruby's count is just a superset of NSArray -count, they're not really incompatible. Now, maybe I'm overlooking something. But I think this could be made to work. So I was just wondering if I should file a bug or if this works as expected.
However, I do not understand where #count is used in your snippet. Is it used in Pathname?
Oh, sorry about the confusion. I just posted that an an example to show that after performing a few ruby method calls that return new arrays (map, compact) on an NSArray, I'd expect to have a Ruby array. The entire script where I got that from is here: http://github.com/kch/mac-user-scripts/blob/master/Applications/Finder/Paste... You can see the count call later in the code.
participants (2)
-
Caio Chassot
-
Laurent Sansonetti