Skip to content

Instantly share code, notes, and snippets.

@kch
Created September 29, 2010 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kch/602174 to your computer and use it in GitHub Desktop.
Save kch/602174 to your computer and use it in GitHub Desktop.
NSArrays are tricky bastards
#!/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