Skip to content

Instantly share code, notes, and snippets.

@jasonporritt
Created November 15, 2012 21:28
RubyMotion count method with NSArray vs. Array
describe Array do
it "calls the Rubyish count method on Rubyish arrays" do
count = [1,2,3,4,5,6].count { |i| i > 3 }
count.should == 3
end
it "doesn't call the Rubyish count method on NSArray objects" do
nsarray = NSArray.arrayWithArray([1,2,3,4,5,6])
nsarrayCount = nsarray.count { |i| i > 3 }
# The block above gets ignored and a warning is printed out in the console, so...
nsarrayCount.should == 6 # WAT?? Yes, 6. Not 3.
end
it "doesn't use the Rubyish count, but I can deal with that using reject and length" do
nsarray = NSArray.arrayWithArray([1,2,3,4,5,6])
nsarrayCount = nsarray.reject { |i| i <= 3 }.length
nsarrayCount.should == 3
end
it "doesn't use the Rubyish count, but I can deal with that using flatten to convert it" do
nsarray = NSArray.arrayWithArray([1,2,3,4,5,6])
notNsArray = [nsarray].flatten
notNsArray.count { |i| i > 3 }.should == 3
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment