Created
September 29, 2010 01:53
-
-
Save kch/602174 to your computer and use it in GitHub Desktop.
NSArrays are tricky bastards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env macruby | |
framework 'Cocoa' | |
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) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment