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

Error malloc(): memory corruption nginx with passenger?

Error malloc(): memory corruption nginx with passenger Passenger issue resolving steps :  sudo gem uninstall passenger(uninstall all passenger) sudo gem install passenger sudo passenger-install-nginx-module --auto --auto-download --prefix=/opt/nginx --extra-configure-flags=none Update nginx config file with new passenger version and restart the nginx

PayUMoney Integration in Ruby on Rails

PayUMoney Integration in Ruby on Rails Generating Cheksum in Payu    Hash or Checksum = sha512(key|txnid|amount|productinfo|firstname|email|u df1|udf2|udf3|udf4|udf5||||||salt) Controller Code Will be def payumoney   @key = "gtKFFx"   @salt =  "eCwWELxi"   val = "#{key|txnid|amount|productinfo|firstname|email|u df1|udf2|udf3|udf4|udf5||||||#{@salt}"  @checksum = Digest::SHA512.hexdigest("#{val}") end <form action="https://test.payu.in/_payment" method='post' id='sub'  >     <input type="hidden" name="key" value="<%=@key%>">     <input type="hidden" name="hash" value="<%=@checksum%>">     <input type="hidden" name="txnid" value="TXN1234">     <input type="hidden" name="amount" value="200">     <input type="hidden" name=...

RSPEC TESTCASES FOR MODEL VALIDATIONS IN RAILS

describe User do    before(:each) do     @user_new = { :name => "chinna",:email => "chinna@yopmail.com",:password => "chinnaram",:password_confirmation => "chinnaram"}    end it "should create a new user details" do      @user_attr = User.create!(@user_new) end ##validations in your model ########### User.rb(your model your mention like that....) validates :name,:email,:presence => true validates :name,:uniqueness => true validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png','image/pjpeg','image/x-png','image/gif'] ###validation for name presence it "should require a name" do      user = User.new(@user_attr.merge(:name => ""))      user.should_not be_valid   end #validation for email presence  it "should require a email" do      user = User.new(@user_attr.merge(:email => ""))      user...