13

Is there any difference between array.count and array.size?

I am getting errors sometimes on production environment while using array.count, but the same works fine when I use array.size.

1
  • 2
    There was a good post about this a few years back: blog.hasmanythrough.com/2008/2/27/count-length-size Commented Dec 9, 2010 at 16:51

1 Answer 1

28

Starting from Ruby 1.8.7, Array#count is the same of Array#size.

Please note that Array#count doesn't exist in Ruby versions lower than 1.8.7 so if you are running Ruby 1.8.6 in production, it will raise a NoMethodError.

Also, make sure you are talking about arrays. In ActiveRecord, for example, #size and #count are slightly different. #size understands caching like counter cache, while count doesn't.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that. Now only i realized my development system has ruby 1.8.7 and staging had 1.8.6.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.