{"id":1025,"date":"2013-03-09T13:05:46","date_gmt":"2013-03-09T21:05:46","guid":{"rendered":"http:\/\/www.wiredatom.com\/blog\/?p=1025"},"modified":"2013-03-09T13:05:46","modified_gmt":"2013-03-09T21:05:46","slug":"rubys-length-vs-size-vs-count-methods","status":"publish","type":"post","link":"https:\/\/www.wiredatom.com\/blog\/2013\/03\/09\/rubys-length-vs-size-vs-count-methods\/","title":{"rendered":"Ruby&#8217;s #length vs #size vs #count Methods"},"content":{"rendered":"<p>Before googling for the article, I knew going in that <code class=\"codecolorer text default\"><span class=\"text\">.size<\/span><\/code> is an alias to <code class=\"codecolorer text default\"><span class=\"text\">.length<\/span><\/code>. But I didn&#8217;t know there was more to the story. And what about <code class=\"codecolorer text default\"><span class=\"text\">.count<\/span><\/code>?<\/p>\n<p>Here&#8217;s what <a href=\"http:\/\/blog.hasmanythrough.com\/2008\/2\/27\/count-length-size\" title=\"Josh Susser\" target=\"_blank\">Josh Susser<\/a> wrote verbatim:<\/p>\n<blockquote><p>\nIn Ruby, #length and #size are synonyms and both do the same thing: they tell you how many elements are in an array or hash. Technically #length is the method and #size is an alias to it.<\/p>\n<p>In ActiveRecord, there are several ways to find out how many records are in an association, and there are some subtle differences in how they work.<\/p>\n<p>* post.comments.count &#8211; Determine the number of elements with an SQL COUNT query. You can also specify conditions to count only a subset of the associated elements (e.g. :conditions => {:author_name => &#8220;josh&#8221;}). If you set up a counter cache on the association, #count will return that cached value instead of executing a new query.<\/p>\n<p>* post.comments.length &#8211; This always loads the contents of the association into memory, then returns the number of elements loaded. Note that this won&#8217;t force an update if the association had been previously loaded and then new comments were created through another way (e.g. Comment.create(&#8230;) instead of post.comments.create(&#8230;)).<\/p>\n<p>* post.comments.size &#8211; This works as a combination of the two previous options. If the collection has already been loaded, it will return its length just like calling #length. If it hasn&#8217;t been loaded yet, it&#8217;s like calling #count.<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Before googling for the article, I knew going in that .size is an alias to .length. But I didn&#8217;t know there was more to the story. And what about .count? Here&#8217;s what Josh Susser wrote verbatim: In Ruby, #length and #size are synonyms and both do the same thing: they tell you how many elements &hellip; <a href=\"https:\/\/www.wiredatom.com\/blog\/2013\/03\/09\/rubys-length-vs-size-vs-count-methods\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Ruby&#8217;s #length vs #size vs #count Methods&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[21,10,4],"tags":[],"class_list":["post-1025","post","type-post","status-publish","format-standard","hentry","category-coding","category-geek-stuff","category-tips-tricks"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p54IqZ-gx","_links":{"self":[{"href":"https:\/\/www.wiredatom.com\/blog\/wp-json\/wp\/v2\/posts\/1025","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wiredatom.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wiredatom.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wiredatom.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wiredatom.com\/blog\/wp-json\/wp\/v2\/comments?post=1025"}],"version-history":[{"count":0,"href":"https:\/\/www.wiredatom.com\/blog\/wp-json\/wp\/v2\/posts\/1025\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wiredatom.com\/blog\/wp-json\/wp\/v2\/media?parent=1025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wiredatom.com\/blog\/wp-json\/wp\/v2\/categories?post=1025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wiredatom.com\/blog\/wp-json\/wp\/v2\/tags?post=1025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}