[MacRuby-devel] NSArray's count and Ruby Array#count
Caio Chassot
lists at caiochassot.com
Mon Oct 4 00:14:42 PDT 2010
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"?
More information about the MacRuby-devel
mailing list