<?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: Pavs</title>
    <description>The latest articles on DEV Community by Pavs (@pabloadell).</description>
    <link>https://dev.to/pabloadell</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%2F710763%2F3e8eb982-d3da-45d2-b9d2-e5f63b9d0cf1.jpeg</url>
      <title>DEV Community: Pavs</title>
      <link>https://dev.to/pabloadell</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pabloadell"/>
    <language>en</language>
    <item>
      <title>Ruby chip cookie: Loading and Requiring</title>
      <dc:creator>Pavs</dc:creator>
      <pubDate>Sat, 25 Sep 2021 11:15:33 +0000</pubDate>
      <link>https://dev.to/pabloadell/ruby-chip-cookie-loading-and-requiring-ee1</link>
      <guid>https://dev.to/pabloadell/ruby-chip-cookie-loading-and-requiring-ee1</guid>
      <description>&lt;p&gt;I was reading about namespaces in Ruby the other day and came across an interesting point where it compared &lt;code&gt;require&lt;/code&gt; and &lt;code&gt;load&lt;/code&gt; . &lt;/p&gt;

&lt;p&gt;Since I started writing Ruby, I did not though about the secrets behind the usage of &lt;code&gt;require&lt;/code&gt; when other languages use &lt;code&gt;load&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's suppose we have a file called &lt;code&gt;weather_forecast.rb&lt;/code&gt; , that just has some functionality in it for returning weather forecasting information.&lt;br&gt;
In order to execute it in our code, we could do something like &lt;code&gt;load('weather_forecast.rb')&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Everything looks good up to this point. We load the code, use it in our program, and move on with our lives. &lt;/p&gt;

&lt;p&gt;However, the constants that the file defined, are not removed once the file has loaded, and can pollute our program. &lt;/p&gt;

&lt;p&gt;To solve this problem, we could load the file as such &lt;code&gt;load('weather_forecast.rb', true)&lt;/code&gt;. The &lt;code&gt;true&lt;/code&gt; argument, loads the file wrapping it around an anonymous namespace containing all constants that will be destroyed after the code is loaded.&lt;/p&gt;

&lt;p&gt;On the other hand, we find &lt;code&gt;require&lt;/code&gt;. It also loads code, but rather than loading it for execution, as &lt;code&gt;load&lt;/code&gt; does, it imports the library avoiding those leftover class names. &lt;/p&gt;

&lt;p&gt;As an additional fact, &lt;code&gt;require&lt;/code&gt; tries only once to load the file, while &lt;code&gt;load&lt;/code&gt; does it every time the file is executed.&lt;/p&gt;

</description>
      <category>ruby</category>
    </item>
    <item>
      <title>ruby_cool_kid.rb — Meta Programming series: Dynamic dispatch</title>
      <dc:creator>Pavs</dc:creator>
      <pubDate>Tue, 21 Sep 2021 21:56:20 +0000</pubDate>
      <link>https://dev.to/pabloadell/rubycoolkid-rb-meta-programming-series-dynamic-dispatch-2j07</link>
      <guid>https://dev.to/pabloadell/rubycoolkid-rb-meta-programming-series-dynamic-dispatch-2j07</guid>
      <description>&lt;p&gt;Today, I want to start a new series of stories called: &lt;em&gt;“ruby_cool_kid.rb — Meta Programming series”&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;As of right now, I am reading a book called &lt;a href="https://www.amazon.es/Metaprogramming-Ruby-Program-Like-Facets/dp/1941222129/ref=sr_1_1?__mk_es_ES=%C3%85M%C3%85%C5%BD%C3%95%C3%91&amp;amp;dchild=1&amp;amp;keywords=Metaprogramming+Ruby+2%3A+Program+Like+the+Ruby+Pros&amp;amp;qid=1632052597&amp;amp;sr=8-1" rel="noopener noreferrer"&gt;“Metaprogramming Ruby 2: Program Like the Ruby Pros” by Paolo Perrotta&lt;/a&gt;, where he talks about meta programming in Ruby and its advantages.&lt;/p&gt;

&lt;p&gt;As concepts in meta programming are abstract, and sometimes hard to grasp, I am starting this series to try and explain the concepts I learned in an easy way that can be easier understood. Also, it would work as a challenge for me to see if I understood what I learned.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Please note that I will not be following Perrotta’s table of contents. I will follow my inspiration.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;After this short purpose introduction, let’s dive into it!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ruby&lt;/strong&gt;, as opposed to static programming languages like Java or C, &lt;strong&gt;is a dynamic programming language.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ruby is interpreted, not compiled&lt;/strong&gt;, meaning that it &lt;strong&gt;can do at runtime what many static programming languages do at compilation&lt;/strong&gt;, for example, defining new classes or methods. You might be wondering, &lt;em&gt;what does dynamic dispatching have to do with all this.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Well, &lt;strong&gt;dynamic dispatching is the practice of dynamically calling a method based on data or information not known before.&lt;/strong&gt; The main idea that makes the engine work is the method &lt;code&gt;send&lt;/code&gt; (&lt;a href="https://apidock.com/ruby/Object/send" rel="noopener noreferrer"&gt;reference&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;In Ruby, methods are not called on objects but rather method’s names are sent to a receiver, usually the current object.&lt;/p&gt;

&lt;p&gt;As Alan Kay, one of the fathers of the idea of OOP used to say referring to OOP: &lt;em&gt;“The big idea is sending”&lt;/em&gt; (&lt;a href="https://softwareengineering.stackexchange.com/questions/264697/alan-kay-the-big-idea-is-messaging" rel="noopener noreferrer"&gt;reference&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Based on this idea, &lt;code&gt;send&lt;/code&gt; is a method that helps us call object methods without having to use &lt;em&gt;dot notation&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg28dknofe2uczeja6xe1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg28dknofe2uczeja6xe1.png" alt="1_SIaLAr2sweexc7OXDQoG8Q"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Dynamic dispatching, takes advantage of this flexibility to call methods based on the data received. Let’s see an example.&lt;/p&gt;

&lt;p&gt;Suppose you wanted to write a small program to congratulate your mom or dad on their birthday. We could write a little piece of code that would do so dynamically.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy00it7f1n6nalk0em8i1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy00it7f1n6nalk0em8i1.png" alt="1_N3NE49hVLv82ppRQ7qlUcQ"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Of course, this is a very simple example that could have been done without dynamic dispatching, but it can be highly improved using Ruby’s tricks.&lt;/p&gt;

&lt;p&gt;One of the goals or main definitions of meta programming is: &lt;strong&gt;&lt;em&gt;Writing code that writes code&lt;/em&gt;&lt;/strong&gt;, so, let’s try it. &lt;/p&gt;

&lt;p&gt;In the previous example, there is a lot of duplicated code, that can be simplified by using &lt;code&gt;define_singleton_method&lt;/code&gt; (&lt;a href="https://apidock.com/ruby/v2_5_5/Object/define_singleton_method" rel="noopener noreferrer"&gt;reference&lt;/a&gt;). It takes advantage of Ruby’s dynamic nature to define singleton methods. The code above is nice and works, but, what if we got confident and also wanted to congratulate our sister, our brother, our grandma? Let’s sharpen it: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ini6l81ji2bytf9l56i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ini6l81ji2bytf9l56i.png" alt="1_K9dPtIQNQlr3i9H_GacbTw"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see, now &lt;code&gt;define_congratulations&lt;/code&gt; is called whenever the &lt;code&gt;BirthdayCongratulations&lt;/code&gt; class is instantiated. This method calls &lt;code&gt;define_singleton_method&lt;/code&gt;, which takes a method’s name and a block and creates a method just for our object. Then, by using dynamic dispatching, we can call such a method. &lt;/p&gt;

&lt;p&gt;This has been a short concept example on how to use dynamic dispatching on Ruby. While it may not apply to all scenarios, it cannot be argued that it is a really powerful tool to have on our belt.&lt;/p&gt;

&lt;p&gt;Also, take into account that despite being widely used by Ruby developers, send is not as good as it looks, as it exposes also private methods for the class. Use &lt;code&gt;public_send&lt;/code&gt; (&lt;a href="https://apidock.com/ruby/Object/public_send" rel="noopener noreferrer"&gt;reference&lt;/a&gt;) instead if you want to make sure your private methods remain private.&lt;/p&gt;

&lt;p&gt;Did you think this code could not be further improved? It sure can! Follow me along for the next story &lt;em&gt;ruby_cool_kid.rb — Metaprogramming series: Ghost methods&lt;/em&gt; 👻 where I will explain how missing methods can be found 😉&lt;/p&gt;

&lt;p&gt;See you around!! 👋&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>metaprogramming</category>
    </item>
    <item>
      <title>How to create easy Spreadsheets with FastExcel Gem</title>
      <dc:creator>Pavs</dc:creator>
      <pubDate>Tue, 21 Sep 2021 21:48:20 +0000</pubDate>
      <link>https://dev.to/pabloadell/how-to-create-easy-spreadsheets-with-fastexcel-gem-enb</link>
      <guid>https://dev.to/pabloadell/how-to-create-easy-spreadsheets-with-fastexcel-gem-enb</guid>
      <description>&lt;p&gt;Spreadsheets are nowadays one of the most common ways of showing and sharing organised information into a file. Whether it is to keep track of the inventory of goods in a store, or the total transactions performed by a user, spreadsheets will show the organised information in such way that it is clear.&lt;/p&gt;

&lt;p&gt;One day, one of my managers gave me the task of allowing a store to download its entire inventory in a spreadsheet, having multiple pages regarding the different categories the user may have.&lt;br&gt;
After doing some research on top popular gems used in Ruby on Rails for accomplishing this task, I ended up choosing &lt;a href="https://github.com/Paxa/fast_excel" rel="noopener noreferrer"&gt;FastExcel&lt;/a&gt;, that is based on Libxlsxwriter and wraps some methods around it, like formatting cells.&lt;br&gt;
Even though there were some other valid gems, like &lt;a href="https://github.com/caxlsx/caxlsx_rails" rel="noopener noreferrer"&gt;Caxlsx&lt;/a&gt; or &lt;a href="https://github.com/cxn03651/write_xlsx" rel="noopener noreferrer"&gt;Write_xlsx&lt;/a&gt; , FastExcel seemed the most lightweight gem as well as the one that offered the best performance as it is shown under its &lt;a href="https://github.com/Paxa/fast_excel#benchmarks" rel="noopener noreferrer"&gt;benchmarks&lt;/a&gt;&lt;br&gt;
...&lt;/p&gt;

&lt;h1&gt;
  
  
  Case Scenario
&lt;/h1&gt;

&lt;p&gt;In order to show how the gem can be exploited, let’s suppose a scenario where a store has 3 categories of products each one of them holding 10 to 25 products each.&lt;/p&gt;

&lt;h2&gt;
  
  
   Base Case
&lt;/h2&gt;

&lt;p&gt;The first task to accomplish is creating a spreadsheet that has three sheets, each one of them containing the products for each category. Let’s see how that’d go:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fit11lv9jr9kkxe9jk5x2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fit11lv9jr9kkxe9jk5x2.png" alt="1_dYBvnl8BMlxSA-MnADemiw"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s see what have we done here:&lt;/p&gt;

&lt;p&gt;First, we have &lt;strong&gt;created a workbook&lt;/strong&gt; named &lt;code&gt;inventory.xlsx&lt;/code&gt; , that will be the file where we’ll write our data. After that, we loop on the different product categories the store has to offer, &lt;strong&gt;creating a new spreadsheet&lt;/strong&gt; for each of them by using &lt;code&gt;add_worksheet(sheet_name)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Diving into the data to be written, the headers are to be added first, formatting them in bold to highlight them. To do so, we have &lt;strong&gt;appended the headers by using&lt;/strong&gt; &lt;code&gt;append_row(data, format&lt;/code&gt; , and formatted them in bold by calling the bold_format method on our FastExcel object, that is basically a wrapper method around Libxlsxwriter’s set format method in order to format cells to bold.&lt;/p&gt;

&lt;p&gt;Finally, we have looped inside the products for each category, appending them to the worksheet. Once all categories are filled, the workbook needs to be closed in order for us to access to it, as the &lt;strong&gt;&lt;code&gt;constant_memory: true&lt;/code&gt;&lt;/strong&gt; that we used for creating the file, &lt;strong&gt;saves each row to disk&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzy8pgp7vacg1tqfb5ajv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzy8pgp7vacg1tqfb5ajv.png" alt="1_Z3Swgu5KKZRoOba8COnpRA"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
   Advanced functions
&lt;/h2&gt;

&lt;p&gt;Even though the base case would be a good fit for an inventory report, let’s image we are also asked for a total sum of the products we have in the shelves, so that we know how many items could be expected to sell.&lt;/p&gt;

&lt;h3&gt;
  
  
  Formulas
&lt;/h3&gt;

&lt;p&gt;Of course, we could apply a simple &lt;code&gt;SUM(ROW_A:ROW_B)&lt;/code&gt; formula on the file once it has been generated but, what if we could append it and save time?&lt;br&gt;
Let’s see it, &lt;strong&gt;formulas are a special value type in excel and FastExcel has wrapped their behaviour under its own &lt;a href="https://github.com/Paxa/fast_excel/blob/8fafe6f20cf51c683314a589f1597113d8704b0a/lib/fast_excel.rb#L9" rel="noopener noreferrer"&gt;class&lt;/a&gt;&lt;/strong&gt;, in order to ease its inclusion.&lt;/p&gt;

&lt;p&gt;Supposing we wanted to include our formula to the end and the units for each product are on row &lt;code&gt;B&lt;/code&gt;, the code for including it in the worksheet would be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmfnlr7467uot4ckjvqwf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmfnlr7467uot4ckjvqwf.png" alt="1_eAVHnG5bQKTISE5ZAnjjdQ"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8pqwaogpnfn4zxtnt1dn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8pqwaogpnfn4zxtnt1dn.png" alt="1_TYy0U1cnJFIKhmKo9P9v5w"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After this, we can have a better understanding of the gem and how easy it is to create xlsx files from rails and even apply formulas to them.&lt;/p&gt;

&lt;p&gt;I hope this is of help for anybody looking for a quick xlsx file generator.&lt;/p&gt;

&lt;p&gt;I will se you around! Keep grinding !&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>fastexcel</category>
    </item>
  </channel>
</rss>
