Skip to main content

Difference between Array Push and Array Insert

Difference between Array Push and Array Insert ?

As we all know there are more than one way of doing the same thing in the Ruby. Thus, it becomes more important to choose the right way so that it does not affect your Performance

Now, let us discuss about Arrays in Ruby.

=> Array is collection of entities

=> Array can be the collection of different type of objects like, integers, strings, hashes, symbols etc.

=> Methods: There are many inbuilt methods that come in handy for doing particular task

=> One can create Array and get list of the methods that can be called on Array object in Ruby by following command,

Creating Empty Array

array = Array.new   # where array is name of object

Listing Methods on Array

array.methods.sort # where array is object of Array class

=> Explore the various methods on Array class.

=> You can refer documentation of Ruby Array here.

Array Push and Array Insert

array = []   # Another way of creating an Array

=> We can insert an element is Array by,

array << 4   # Inserted element 4 in the Array, thus it will be,  [4]

=> We can also push element in the Array by,

 array.push(5)  
# Pushed (Ultimately inserted) element in the Array, thus it  will be, [4,5]



Both of the methods look exactly same and ultimately they achieve the same result of pushing/inserting an element in the array. But, the difference is

 => Array.push  can be used to insert more than one element in Array at a time. e.g.

array.push(1,2,3)

while,

=> Insertion operator (<<) can be used to insert only one element at a time in the Array.
Performance Consideration:

If we consider performance, then

Case: Want to add single element in the Array => Prefer using Insertion Operator(<<) than using Array.push method. Benchmarking proves the Fact.

Case: Want to add multiple elements in Array => You can prefer Array.push method.

That was all regarding Array Push and Insert, play with methods of Array object to get familiar with. We will get to know about Benchmarking in our upcoming post.

Comments and Suggestions are Welcome!

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