<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Harish Reddy Vantikommu on Medium]]></title>
        <description><![CDATA[Stories by Harish Reddy Vantikommu on Medium]]></description>
        <link>https://medium.com/@harishreddyvantikommu?source=rss-d0c293d8fa51------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*WCW51UUm4yKcCx9UsNQJfQ.jpeg</url>
            <title>Stories by Harish Reddy Vantikommu on Medium</title>
            <link>https://medium.com/@harishreddyvantikommu?source=rss-d0c293d8fa51------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 10:51:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@harishreddyvantikommu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Ruby | Enumerable: take_while vs drop_while methods]]></title>
            <link>https://medium.com/@harishreddyvantikommu/ruby-enumerable-take-while-vs-drop-while-methods-483f98968d28?source=rss-d0c293d8fa51------2</link>
            <guid isPermaLink="false">https://medium.com/p/483f98968d28</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[enumerable]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[arrays]]></category>
            <dc:creator><![CDATA[Harish Reddy Vantikommu]]></dc:creator>
            <pubDate>Thu, 22 Jun 2023 04:35:41 GMT</pubDate>
            <atom:updated>2023-08-25T03:38:39.544Z</atom:updated>
            <content:encoded><![CDATA[<p>In Ruby, the take_while and drop_while methods are both used to extract elements from an array based on a certain condition. However, they differ in their behavior and the resulting output.</p><h4>take_while:</h4><ul><li>take_while returns a new array that contains elements from the beginning of the original array until the specified condition is no longer met.</li><li>The method iterates over the elements of the array and stops as soon as the condition evaluates to false or nil.</li><li>The resulting array will include all the elements up to the point where the condition is no longer satisfied.<br># Using each method and break statement<br>a = [4, 5, 8, 7, 6]<br>result = []<br>a.each do |e|<br> break unless e &lt; 8<br> result &lt;&lt; e<br>end<br><br>puts result<br>output =&gt; [4, 5]<br><br># Using take_while method<br>a = [4, 5, 8, 7, 6]<br>result = a.take_while { |e| e &lt; 8 }<br>puts result<br>output =&gt; [4, 5]</li></ul><h4>drop_while:</h4><ul><li>drop_while returns a new array that excludes elements from the beginning of the original array until the specified condition is no longer met</li><li>The method iterates over the elements of the array and skips elements as long as the condition evaluates to <strong>true</strong></li><li>The resulting array will include all the elements starting from the point where the condition is no longer satisfied</li></ul><pre># Using each method and break statement<br>a = [4, 5, 8, 7, 6]<br>result = []<br>should_add = false<br><br>a.each do |e|<br>  should_add = true if e &gt;= 8<br>  result &lt;&lt; e if should_add<br>end<br>puts result<br>output =&gt; [8, 7, 6]<br><br># Using drop_while method<br>a = [4, 5, 8, 7, 6]<br>result = a.drop_while { |e| e &lt; 8 }<br>puts result<br>output =&gt; [8, 7, 6]</pre><p>In summary, <strong>take_while</strong> takes elements from the beginning until the condition is false, while <strong>drop_while</strong> drops elements from the beginning until the condition is false and includes the remaining elements.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=483f98968d28" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[flat_map: an unpopular built-in Ruby method]]></title>
            <link>https://medium.com/@harishreddyvantikommu/flat-map-an-unpopular-built-in-ruby-method-65ae5f20cb2f?source=rss-d0c293d8fa51------2</link>
            <guid isPermaLink="false">https://medium.com/p/65ae5f20cb2f</guid>
            <category><![CDATA[ruby-methods]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[ror]]></category>
            <dc:creator><![CDATA[Harish Reddy Vantikommu]]></dc:creator>
            <pubDate>Wed, 21 Jun 2023 05:53:18 GMT</pubDate>
            <atom:updated>2023-06-21T08:26:08.917Z</atom:updated>
            <content:encoded><![CDATA[<p>The flat_map method in Ruby is a built-in enumerable method that returns a new array by applying a block to each element in the enumerable and concatenating the results. If no block is provided, it returns an enumerator instead.</p><p><strong><em>Syntax</em></strong><em>: block.flat_map { |obj| block }</em></p><h4><strong>Example 1:</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ro_7d1sDMQ4CCzVv8uU3og.png" /></figure><ol><li>An array enu is initialized with two sub-arrays: [2, 3] and [4, 5].</li><li>The flat_map method is called on enu with the block { |el| el }.</li><li>The block is applied to each element of enu, which represents the sub-arrays [2, 3] and [4, 5].</li><li>Inside the block, each element el represents one of the sub-arrays.</li><li>The block simply returns the element el as is, without any modification.</li><li>The flat_map method combines the resulting elements from each iteration into a single array.</li><li>The final result is an array that contains the elements of the original sub-arrays. The resulting array is [2, 3, 4, 5].</li></ol><h4>Example 2:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B4whkyLhbddjT60ZyMzp7g.png" /></figure><ol><li>An array enu is initialized with three sub-arrays: [1], [2, 3], and [4, 5].</li><li>The flat_map method is called on enu with the block { |el| el + [1000] }.</li><li>The block is applied to each element of enu, which represents the sub-arrays [1], [2, 3], and [4, 5].</li><li>Inside the block, each element el represents one of the sub-arrays.</li><li>The block concatenates the element el with the array [1000], resulting in a new array.</li><li>The flat_map method combines the resulting arrays from each iteration into a single array.</li><li>The final result is an array that contains the elements of the original sub-arrays, followed by 1000 appended to each element. The resulting array is [1, 1000, 2, 3, 1000, 4, 5, 1000].</li></ol><h4><strong>Difference between map{}.flatten vs flat_map</strong></h4><p>In Ruby, map{}.flatten and flat_map are both methods used to transform elements of an enumerable and flatten the resulting nested arrays. However, there is a difference in how they achieve this.</p><h4><strong>1. map{}.flatten:</strong></h4><ul><li>The map method is used to transform each element of an enumerable based on the provided block.</li><li>The block is applied to each element, and the return value of the block is stored in a new array.</li><li>The flatten method is then called on the resulting array to flatten any nested arrays into a single level.</li><li>The final result is a new array where each element is transformed according to the block and any nested arrays are flattened.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*osTndSniC1LnW3NG39Ru9w.png" /></figure><h4><strong>2. flat_map:</strong></h4><ul><li>The flat_map method combines the functionality of map and flatten into a single step.</li><li>It applies a block to each element of an enumerable and flattens the resulting elements into a single array.</li><li>The block is applied to each element, and the return value of the block is stored in a new array.</li><li>The flat_map method automatically flattens any nested arrays that may be returned by the block.</li><li>The final result is a new array where each element is transformed according to the block, and any nested arrays are automatically flattened.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J5nvMLyQgJ4Cz2H3Je41AA.png" /></figure><p><strong>Note</strong>: When working with multi-dimensional arrays (arrays with more than two dimensions like [[1,2], [[3, 4], 5], 6]) , it’s important to note that the flat_map method will not flatten the results into a single array.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=65ae5f20cb2f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tips to improve performance in ROR.]]></title>
            <link>https://medium.com/@harishreddyvantikommu/tips-to-improve-performance-in-ror-5d590583d698?source=rss-d0c293d8fa51------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d590583d698</guid>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Harish Reddy Vantikommu]]></dc:creator>
            <pubDate>Tue, 07 Sep 2021 04:49:07 GMT</pubDate>
            <atom:updated>2021-09-11T02:24:21.226Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong>Ruby is a heavily object-oriented language. In fact, (almost) everything in Ruby is an object. Creating unnecessary objects will cost our program a lot of additional memory usage, so we need to avoid it.</strong></blockquote><p>Here I used the <a href="https://github.com/ruby/benchmark"><strong><em>benchmark</em></strong></a> gem to identify the time taking for execution. and used the <a href="https://github.com/SamSaffron/memory_profiler"><strong><em>memory-profiler</em></strong></a> gem to identify the memory usage.</p><h3><strong>Mainly the application performance depends on two things</strong></h3><ol><li>Number of SQL queries that are firing to the DB</li><li>Memory allocation for the objects when the program is executing</li></ol><h3>Below are the simple and basic tips to improve the performance</h3><p><strong>Tip 1: Use ‘pluck’ instead of ‘map’ to fetch the attributes:</strong></p><blockquote>Pluck and map are shortcuts that help us choose one or several attributes from the records. Let’s compare pluck with map. Map loads objects into memory and gets the attribute. Pluck returns the attributes without loading objects into memory. With pluck, we will get better SQL performance and less time and memory use</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M0zRFX9pANNJB4dWDEk6_A.png" /><figcaption>performance comparison between map and pluck</figcaption></figure><p><strong>Tip 2: Use “size” instead of “length” to fetch the count of records:</strong></p><blockquote>We should not use length to define how many records are there in a table. ActiveRecord doesn’t define a length property it’s an Array property. This means that if we call User.all.length, all user records will get loaded into memory and converted into an array. Whereas size is ActiveRecord Property and won’t load user records into memory instead it will directly perform SQL count query.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K9S_aLfQfZTauOb8PS06iw.png" /></figure><p><strong>Tip 3: Don’t use “count” to find the count if the records are already loaded:</strong></p><blockquote>Ruby also provides the <strong>count </strong>method to fetch the count. This true method determines the number of elements with an SQL COUNT query. If we haven’t loaded any records, we can use <strong>count </strong>to perform a count query on the database. But if the records are already loaded we must not use the count method to fetch the count.</blockquote><blockquote>It performs the sql count query on DB each time we call count</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MqpQ9fEOj3p8RR6uThrV0A.png" /></figure><p><strong>Tip 4: Use append(“&lt;&lt;”) operator to concat the strings</strong>:</p><blockquote>If we use append operator to concat the strings Ruby does not create a new object into memory instead it will modify existing object.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fjEyT0Wsc4a0XERCHqLaTA.png" /></figure><p><strong>Tip 5: Use symbols instead of strings in hashes</strong></p><blockquote>Symbols are a Ruby specific data-type. Symbols are similar to strings, except they are immutable. They are more efficient than strings because they are stored in one spot in memory.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-CqxApMrMwHDe3EfVFK_Rw.png" /></figure><blockquote>As we see in above example, when we set two variables equal to the same symbol, they point to the same address in memory.</blockquote><blockquote>string_hash = { “a” =&gt; 123, “b” =&gt; 234 }</blockquote><blockquote>If we use a string as a Hash key, Ruby needs to evaluate the string and look at it’s contents (and compute a hash function on that) and compare the result against the (hashed) values of the keys which are already stored in the Hash.</blockquote><blockquote>symbol_hash = { a: 123, b: 234 }</blockquote><blockquote>If we use a symbol as a Hash key, it’s implicit that it’s immutable, so Ruby can basically just do a comparison of the (hash function of the) object-id against the (hashed) object-ids of keys which are already stored in the Hash. (much faster)</blockquote><p><strong>Tip 6: Memoization with instance variable</strong></p><blockquote>Memoization is a <strong>technique you can use to speed up your accessor methods</strong>. It caches the results of methods that do time-consuming work, work that only needs to be done once. In Rails, you see memoization used so often that it even included a module that would memoize methods for you</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8AV16vIJpXyzizw7nN_hQA.png" /><figcaption>without memoization</figcaption></figure><blockquote><em>This is a bad way because every time we call the organization_name method, a </em>query will be made to the database to find out the organization name of the user. We can avoid querying the db after the first call by saving the result in an instance variable.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nvWCc7Xo4RuwcIs9lpT1Gw.png" /><figcaption>with memoization</figcaption></figure><blockquote>If you see the both implementations the “memoization implementation” fired the the SQL query only once.</blockquote><p><strong>Tip 7: Use bang! methods to avoid creating unnecessary objects</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pmPpeo_6cDTWXjIJHEkK1w.png" /></figure><blockquote>Here the new object created into the memory for downcase results. to avoid creating unnecessary we should use bang method as below.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ArDrPVNO9sTfNVWBZRovqg.png" /></figure><blockquote>Ruby modified existing object instead of creating a new object into the memory when using bang method.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d590583d698" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>