<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kiran Mahale</title>
    <description>The latest articles on DEV Community by Kiran Mahale (@kiranmahale).</description>
    <link>https://dev.to/kiranmahale</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F312671%2F57cc07c9-4e85-4dd5-acaa-b3369efdd805.jpeg</url>
      <title>DEV Community: Kiran Mahale</title>
      <link>https://dev.to/kiranmahale</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kiranmahale"/>
    <language>en</language>
    <item>
      <title>Working Of Array in Ruby</title>
      <dc:creator>Kiran Mahale</dc:creator>
      <pubDate>Fri, 07 May 2021 11:20:36 +0000</pubDate>
      <link>https://dev.to/kiranmahale/working-of-array-in-ruby-b0a</link>
      <guid>https://dev.to/kiranmahale/working-of-array-in-ruby-b0a</guid>
      <description>&lt;p&gt;Today, we are going to see how ruby adds elements into an array without specifying the size. Let’s start.&lt;br&gt;
When we define an array in C then we create it for a particular size, right?&lt;br&gt;
&lt;code&gt;int array[ 10 ];&lt;/code&gt;&lt;br&gt;
Here, we have defined an integer array &lt;code&gt;array&lt;/code&gt; of size 10. So it will contain a maximum of 10 numbers. If we try to insert/push more than 10 elements then it will give an error. To add more element you need to increase the size of the array &lt;code&gt;array&lt;/code&gt;.&lt;br&gt;
But, in Ruby, we never define the size of the array, so how it is handling?&lt;/p&gt;

&lt;p&gt;Following is the strategy that Ruby uses while creating an array and adding elements to it.&lt;br&gt;
Whenever we define an array in ruby then it defines its size as 3 by default.&lt;br&gt;
&lt;code&gt;array = []&lt;/code&gt;&lt;br&gt;
means in C&lt;br&gt;
&lt;code&gt;int array[3] = [];&lt;/code&gt;&lt;br&gt;
So what will happen when we push more than 3 elements in array? It will create a new array of size 20 and copy all the elements from the old array to a new array and then it will insert a new element to it. So, ruby calculates the size of the new array when it is full, like 3, 20, 37, 56, 85 …..&lt;br&gt;
You can check the following link which describes how they are calculating the size of the array when it is full and when we push a new element to it.&lt;br&gt;
&lt;a href="https://github.com/ruby/ruby/blob/ruby_2_0_0/array.c#L183"&gt;https://github.com/ruby/ruby/blob/ruby_2_0_0/array.c#L183&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>array</category>
    </item>
  </channel>
</rss>
