On 24/06/15 22:07, Suliman wrote:

If not to look at docs I would say that size is bite size, count -
number of elements. So what is length I can't say. Probably I missed
length and count.

"length" and "size" are the exact same thing. They return the number of elements of an array or string. "count" is a bit more versatile, it can be called without parameters, with one parameter or with a block. If a parameter is given it will count the number of occurrences of that element. If a block is given it will pass each element to the block and count how many times it returns true. If no parameter or block is given it will work exactly like "length" and "size".

It gets event more interesting if you add ActiveRecord to the mix. The result of a query in ActiveRecord will return some form of object that acts like an array. Example:

Person.where(name: 'John').length

Will get all rows matching "John" and return how many. This on the other hand:

Person.where(name: 'John').count

Will actually perform a count query, avoid loading all objects in memory.

--
/Jacob Carlborg

Reply via email to