Skip to main content

Ruby Count vs Length vs Size

Ruby Count vs Length vs Size ?


Ruby Count vs Length vs Size -

These methods are used on Arrays, Hashes or Objects. This tutorial will illustrate difference between count length and size.
1. Array#length

   Basically length method used on arrays in ruby returns number of elements in the array for which method is invoked.
Example, suppose we have array a as,

a = [1,2,3,4,5,6,7]

Calling Array#length method on array will give result as,

a.length
=> 7

For more reference on how length is calculated, refer source code for the length on apidock for Array#length which shows that it uses RARRAY_LEN(array) method to get length.
2. Array#size

Array#size is just an alias to the Array#length method. It executes same Array#length method internally.
Example,

b = [1,2,3,4]
b.size
=> 4

3. Array#count

Count has some more functionalities than length/size. It can be used for getting number of elements based on some condition. Count can be called in three ways:
Sr. No.     Method     Description
1.     Array#count     Returns number of elements in Array
2.     Array#count n     Returns number of elements having value n in Array
3.     Array#count{|i| i.even?}     Returns count based on condition invoked on each element array

Example:
Suppose we have array c as,

c = [1,2,3,4,4,7,7,7,9]

Method 1:

c.count
=> 9

Method 2:

c.count 7
=> 3

As there are 3 elements in the array having value = 3

Method 3:

c.count {|i| i>5}
=> 4

As there are 4 elements in the array having value greater than 5.

These variants are supported by the count method because it actually iterated through each element in the array and thus allows conditional count of the elements in the array.

You can refer source code on apidock for Array#count method for more information.
Conclusion -

   The concept of ruby count vs length vs size was clarified in this tutorial. Few notes on when each of these methods should be used while coding:

1. Array#count should be used only for getting number of elements in array based on some condition.
2. Otherwise, for getting number of elements without any condition use Array#length or Array#size.

Comments

Popular posts from this blog

Create dynamic sitemap on ruby on rails

Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site. It’s basically a XML file describing all URLs in your page: The following example shows a Sitemap that contains just one URL and uses all optional tags. The optional tags are in italics. <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">    <url>       <loc>http://www.example.com/</loc>       <lastmod>2005-01-01</lastmod>       <changefreq>monthly</changefreq>     ...

Omniauth Linked in Ruby On Rails

def get_linkedin_user_data      omniauth = request.env["omniauth.auth"]      dat=omniauth.extra.raw_info      linked_app_key = "xxxxxxx"      linkedin_secret_key = "yyyyyyy"      client = LinkedIn::Client.new(linked_app_key,linkedin_secret_key)      client.authorize_from_access(omniauth['credentials']['token'],omniauth['credentials']['secret'])      connections=client.connections(:fields => ["id", "first-name", "last-name","picture-url"])      uid=omniauth['uid']      token=omniauth["credentials"]["token"]      secret=omniauth["credentials"]["secret"]   #linked user data     omniauth = request.env["omniauth.auth"]      data             = omniauth.info      user_name...

Install Rvm on ubuntu

sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev curl -L https://get.rvm.io | bash -s stable source ~/.rvm/scripts/rvm rvm install 2.0.0-p645 rvm use 2.0.0-p645 --default ruby -v rvm gemset create rails3.2.8 rvm gemset use rails3.2.8 gem install rails -v 3.2.8