<?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: Magesh</title>
    <description>The latest articles on DEV Community by Magesh (@magesh).</description>
    <link>https://dev.to/magesh</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%2F84846%2F63f6ec21-7eca-4dd1-b33b-102e0d560e8b.jpg</url>
      <title>DEV Community: Magesh</title>
      <link>https://dev.to/magesh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/magesh"/>
    <language>en</language>
    <item>
      <title>Meta programming with Ruby Eval: A guide (Part 1)</title>
      <dc:creator>Magesh</dc:creator>
      <pubDate>Mon, 06 Jan 2025 05:00:00 +0000</pubDate>
      <link>https://dev.to/railsfactory/meta-programming-with-ruby-eval-a-guide-part-1-3ndn</link>
      <guid>https://dev.to/railsfactory/meta-programming-with-ruby-eval-a-guide-part-1-3ndn</guid>
      <description>&lt;p&gt;During one of our bootcamp study sessions, we explored Ruby's eval and discovered how powerful meta-programming in Ruby can be. It enables you to do almost anything at runtime, from evaluating complex expressions to creating classes, modules, methods, variables, and constants dynamically.&lt;/p&gt;

&lt;p&gt;For starters, what is meta programming?&lt;br&gt;
In simple terms, meta programming is about writing code that can generate more code. &lt;/p&gt;

&lt;p&gt;Let me clarify, you write a code that generates more code during runtime which can then be executed dynamically. &lt;/p&gt;

&lt;p&gt;It is not just about executing a logic like &lt;code&gt;2+2&lt;/code&gt; or &lt;code&gt;(a + b)²&lt;/code&gt;&lt;br&gt;
Or calling a method dynamically like &lt;br&gt;
&lt;code&gt;send(:my_method)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In ruby, you can write code that will create classes, modules and methods,  variables dynamically. It can even extend existing classes/modules and do more. &lt;/p&gt;
&lt;h2&gt;
  
  
  Basic Eval
&lt;/h2&gt;

&lt;p&gt;When I was new to ruby, the only thing I knew about eval was to use it for evaluating simple expressions. I thought that was it; I didn't know it could do more. &lt;/p&gt;

&lt;p&gt;You can use eval to execute arbitrary ruby code contained in a string like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;eval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"2+ 3+5"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While powerful, eval should be used cautiously as it executes code in the current binding and can pose security risks if used with untrusted input.&lt;/p&gt;

&lt;p&gt;That was super simple but you can do much more. Let's explore more.&lt;/p&gt;

&lt;h2&gt;
  
  
  Instance Evaluation
&lt;/h2&gt;

&lt;p&gt;instance_eval allows you to evaluate code in the context of an object's instance. This method is particularly useful when you need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Access instance variables directly&lt;/li&gt;
&lt;li&gt;Define singleton methods on specific instances&lt;/li&gt;
&lt;li&gt;Modify a single object's behaviour&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Dynamically set or get instance variables
&lt;/h2&gt;

&lt;p&gt;Let's define a class with an instance variable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Magesh"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use eval to dynamically set the instance variable like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;eval&lt;/span&gt; &lt;span class="s2"&gt;"person.instance_variable_set(:@name, 'Dinesh')"&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;instance_variable_get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:@name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Output: Dinesh&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use eval to dynamically get the instance variable like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;eval_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;eval&lt;/span&gt; &lt;span class="s2"&gt;"person.instance_variable_get(:@name)"&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;eval_name&lt;/span&gt; &lt;span class="c1"&gt;# Output: Dinesh&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using &lt;code&gt;class_eval&lt;/code&gt; to create class
&lt;/h2&gt;

&lt;p&gt;Let's create a method that can create more classes by taking a name as an argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_dynamic_class&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;methods_to_add&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class_eval&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;-&lt;/span&gt;&lt;span class="no"&gt;RUBY&lt;/span&gt;&lt;span class="sh"&gt;
    class &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;
      attr_reader :created_at
      def initialize(name)
     @name = name
        @created_at = Time.now
      end

    def greet
      "Hello! Welcome"
    end

    #adding a class method
      def self.description
        "I am a dynamically created class named &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"
      end
    end
&lt;/span&gt;&lt;span class="no"&gt;  RUBY&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now whenever I want to create a class I can simply call the method like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;create_dynamic_class&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Dog'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt;

&lt;span class="c1"&gt;# Let's execute the method and check&lt;/span&gt;
&lt;span class="n"&gt;milo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Milo"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;description&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; I am a dynamically created class named Dog&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That was simple, right? Now, how do we add instant methods to that class?&lt;br&gt;
We have to tweak the code a little bit like adding a few more lines to the &lt;code&gt;create_dynamic_class&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Add instance methods using class_eval with a block&lt;/span&gt;
  &lt;span class="n"&gt;klass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;const_get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;# Add each method to the class&lt;/span&gt;
  &lt;span class="n"&gt;methods_to_add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;method_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;method_body&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;klass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class_eval&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;define_method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;method_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;method_body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code will take a hash with method names as keys and procs or blocks as values with the implementation.&lt;/p&gt;

&lt;p&gt;We have to create a hash now to add some methods, let's see how its done&lt;/p&gt;

&lt;p&gt;This is the hash:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;my_methods&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="ss"&gt;greet: &lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"Ruf! Ruf!"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="ss"&gt;say_bye: &lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"Woof! Woof!"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="ss"&gt;birth_time: &lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"Born at: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="vi"&gt;@created_at&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now I have to call&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;create_dynamic_class&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Dog"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_methods&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can test it to check if all the methods are working:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;milo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Milo"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;milo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;
&lt;span class="n"&gt;milo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;say_bye&lt;/span&gt;
&lt;span class="n"&gt;milo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;birth_time&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;that should give you the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;#&amp;lt;Dog:0x0000000110a16650 @created_at=2024-12-30 14:20:08.776167 -0500, @name="Milo"&amp;gt;&lt;/span&gt;
&lt;span class="s2"&gt;"Ruf! Ruf!"&lt;/span&gt;
&lt;span class="s2"&gt;"Woof! Woof!"&lt;/span&gt;
&lt;span class="s2"&gt;"Born at: 2024-12-30 14:22:43 -0500"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See what we did? We have created a class dynamically and added a few instances and class methods to it. All during runtime. That was cool, right? Let's try one more thing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Define singleton methods on specific instances
&lt;/h2&gt;

&lt;p&gt;What if we only want to add methods to a specific instance of a class and not to all the instances(objects). In our code above we created a dog named Milo but what if there is another dog and it could do different things that Milo didn't?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;simba&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Simba"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, what if Simba could roll which Milo couldn't?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;simba&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;instance_eval&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;roll&lt;/span&gt;
    &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="vi"&gt;@name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is rolling over!"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the above method &lt;code&gt;roll&lt;/code&gt; will only be available inside the instance simba.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;simba&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;roll&lt;/span&gt;  &lt;span class="c1"&gt;#=&amp;gt;  "Simba is rolling over!"&lt;/span&gt;

&lt;span class="n"&gt;milo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;roll&lt;/span&gt;   &lt;span class="c1"&gt;#=&amp;gt; undefined method `roll' for an instance of Dog (NoMethodError)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You see, if I call "roll" method on the instance "milo". I would get an undefined method error because we added the roll method only on the instance "simba". That's how you create a singleton method for an instance. &lt;/p&gt;

&lt;p&gt;Great. That's enough for now. I'll show you more in Part 2. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/blog/ruby-meta-programming-eval-part-2"&gt;Go to part 2 to read more&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Meta programming with Ruby Eval: A guide (Part 2)</title>
      <dc:creator>Magesh</dc:creator>
      <pubDate>Mon, 06 Jan 2025 05:00:00 +0000</pubDate>
      <link>https://dev.to/railsfactory/meta-programming-with-ruby-eval-a-guide-part-2-5256</link>
      <guid>https://dev.to/railsfactory/meta-programming-with-ruby-eval-a-guide-part-2-5256</guid>
      <description>&lt;p&gt;Hope you read Part 1 of meta-programming with Ruby eval before coming here, if not you can &lt;a href="https://dev.to/blog/ruby-meta-programming-eval/"&gt;click this link&lt;/a&gt; and read it first. It's kind of like a pre-requisite. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/blog/ruby-meta-programming-eval/"&gt;Meta programming with Ruby Eval - Part 1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In part one, we saw the basics of eval and how to dynamically create a class and add methods to it. Now let's see how we can extend a class, and also learn how to create modules and use them in classes, during runtime. Ready? Let's dive right in. &lt;/p&gt;

&lt;h2&gt;
  
  
  Extending a class
&lt;/h2&gt;

&lt;p&gt;Let's say we already have a class file, we can extend it dynamically during runtime to add more instance and class methods. &lt;/p&gt;

&lt;p&gt;Consider the below example Animal class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To add more methods to it, we can simply use the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;## Adding methods using class_eval&lt;/span&gt;
&lt;span class="no"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class_eval&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;speak&lt;/span&gt;
    &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="vi"&gt;@name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; makes a sound"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;species&lt;/span&gt;
    &lt;span class="vi"&gt;@species&lt;/span&gt; &lt;span class="o"&gt;||=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="c1"&gt;# Add attribute readers dynamically&lt;/span&gt;
  &lt;span class="nb"&gt;attr_reader&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code adds an instance method called "speak", a class method "species", and also an attribute reader to read the &lt;a class="mentioned-user" href="https://dev.to/name"&gt;@name&lt;/a&gt; value. &lt;/p&gt;

&lt;h2&gt;
  
  
  Overriding methods in class or redefining existing behaviour
&lt;/h2&gt;

&lt;p&gt;Now, let's assume the Animal class had a method called "name", like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;name&lt;/span&gt;
    &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="s2"&gt;"The name is &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="vi"&gt;@name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I can override it on the fly using the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class_eval&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="c1"&gt;# Store the original method&lt;/span&gt;
  &lt;span class="kp"&gt;alias_method&lt;/span&gt; &lt;span class="ss"&gt;:original_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;name&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"You can call me &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="vi"&gt;@name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I can use the class_eval to override any method in any class during runtime. But we have to be careful not to change an expected behaviour which might lead to misunderstanding and cause trouble for other developers. &lt;/p&gt;

&lt;h2&gt;
  
  
  Dynamically create Attribute methods
&lt;/h2&gt;

&lt;p&gt;You want to write attribute methods: reader and writer, here's how&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'age'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'color'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'breed'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;attribute&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="no"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class_eval&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="c1"&gt;# Create getter&lt;/span&gt;
    &lt;span class="n"&gt;define_method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attribute&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="nb"&gt;instance_variable_get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"@&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;attribute&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="c1"&gt;# Create setter&lt;/span&gt;
    &lt;span class="n"&gt;define_method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;attribute&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;="&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="nb"&gt;instance_variable_set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"@&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;attribute&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# Usage examples&lt;/span&gt;
&lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Rex"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;speak&lt;/span&gt;              &lt;span class="c1"&gt;# =&amp;gt; "Loudly: Rex makes a sound"&lt;/span&gt;

&lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"brown"&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old and &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;color&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Create Modules using &lt;code&gt;class_eval&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;In part one we saw how to create classes, similarly, can I create modules? and maybe include them in classes? Yes, we can. The following code does that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Creating a module &lt;/span&gt;
&lt;span class="n"&gt;module_code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;-&lt;/span&gt;&lt;span class="no"&gt;RUBY&lt;/span&gt;&lt;span class="sh"&gt;
  module Swimmable
    def swim
      "&lt;/span&gt;&lt;span class="se"&gt;\#&lt;/span&gt;&lt;span class="sh"&gt;{@name} is swimming!"
    end
  end
&lt;/span&gt;&lt;span class="no"&gt;RUBY&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that's the module code but I need to evaluate it first and then include it in a class to make it work, right? So how can we do that?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Evaluate the module code and include it&lt;/span&gt;
&lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class_eval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;module_code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class_eval&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Swimmable&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, I first evaluate the module code using &lt;code&gt;Object.class_eval&lt;/code&gt; and then to include it in a class we used class_eval on the class itself. That's it. Now I can check it by using the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;animal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"panda"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;swim&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That should work. Now you know how to create modules on the fly and include them in any existing class or we can also create a class on the fly and include the module in it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding methods to a module
&lt;/h2&gt;

&lt;p&gt;Let's create a real-world use case. A validation DSL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Validators&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_validator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;validation_logic&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;module_eval&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;define_method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"validate_&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;validation_logic&lt;/span&gt;
          &lt;span class="n"&gt;instance_exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;validation_logic&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
          &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nil?&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
        &lt;span class="k"&gt;end&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use the Validators module to create basic presence validation like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Validators&lt;/span&gt;

  &lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:username&lt;/span&gt;

  &lt;span class="c1"&gt;# Create basic presence validators&lt;/span&gt;
  &lt;span class="n"&gt;create_validator&lt;/span&gt; &lt;span class="ss"&gt;:email&lt;/span&gt;
  &lt;span class="n"&gt;create_validator&lt;/span&gt; &lt;span class="ss"&gt;:age&lt;/span&gt;
  &lt;span class="n"&gt;create_validator&lt;/span&gt; &lt;span class="ss"&gt;:username&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;validate_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;# =&amp;gt; false&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;validate_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"test@example.com"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to create a custom validation to check the price value, etc. You can do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;create_validator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_f&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See how helpful this can be? &lt;/p&gt;

&lt;h2&gt;
  
  
  Extending Modules
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Extensions&lt;/span&gt;
  &lt;span class="nb"&gt;module_eval&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;included&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ClassMethods&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;ClassMethods&lt;/span&gt;
      &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;class_method&lt;/span&gt;
        &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"This is a class method"&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Class and Module Evaluation
&lt;/h2&gt;

&lt;p&gt;module_eval (also aliased as module_exec) is a method that allows you to evaluate code in the context of a module. Like class_eval, it lets you define or modify methods that will be instance methods of classes that include the module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Greeting&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_greeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;module_eval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;-&lt;/span&gt;&lt;span class="no"&gt;RUBY&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;
      def greet_&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;
        puts "Hello, &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;!"
      end
&lt;/span&gt;&lt;span class="no"&gt;    RUBY&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Greeting&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;Greeting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_greeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"alice"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet_alice&lt;/span&gt;  &lt;span class="c1"&gt;# Output: Hello, alice!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While &lt;code&gt;module_eval&lt;/code&gt; and &lt;code&gt;class_eval&lt;/code&gt; are very similar, there are some key differences:&lt;/p&gt;

&lt;p&gt;Context: module_eval is specifically for modules, while class_eval is for classes. However, since classes are also modules in Ruby (Class inherits from Module), you can use module_eval on classes too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# These are equivalent for classes&lt;/span&gt;
&lt;span class="no"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class_eval&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_method&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;module_eval&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_method&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;module_eval is often used in module methods to define methods dynamically that will be available to all classes including that module.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dynamic Method Definition
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;define_method&lt;/code&gt; is a powerful way to create methods dynamically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;APIWrapper&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'get'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'post'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'put'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'delete'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;http_method&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;define_method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http_method&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="c1"&gt;# Generic HTTP request handling&lt;/span&gt;
      &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Making &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;http_method&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upcase&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; request to &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;api&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;APIWrapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/users'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# =&amp;gt; "Making GET request to /users"&lt;/span&gt;
&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/users'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; "Making POST request to /users"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Removing Methods
&lt;/h2&gt;

&lt;p&gt;Just as we can define methods dynamically, we can remove them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;temporary_method&lt;/span&gt;
    &lt;span class="s2"&gt;"I won't be here long"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="n"&gt;remove_method&lt;/span&gt; &lt;span class="ss"&gt;:temporary_method&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Dynamic Constant and Variable Management
&lt;/h2&gt;

&lt;p&gt;Setting Constants&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Configuration&lt;/span&gt;
  &lt;span class="nb"&gt;const_set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:API_VERSION&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"v1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;const_set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:MAX_RETRIES&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="no"&gt;Configuration&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;API_VERSION&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; "v1"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Variable Operations&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StateManager&lt;/span&gt;
  &lt;span class="n"&gt;class_variable_set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:@@state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;state&lt;/span&gt;
    &lt;span class="n"&gt;class_variable_get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:@@state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;update_state&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;instance_variable_set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"@&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Best Practices and Warnings
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Security Considerations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Never use eval with untrusted input&lt;/li&gt;
&lt;li&gt;Prefer more specific evaluation methods over basic eval&lt;/li&gt;
&lt;li&gt;Use define_method instead of eval when defining methods dynamically&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Performance Impact
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Evaluation methods are slower than static definitions&lt;/li&gt;
&lt;li&gt;Cache results when doing repeated evaluations&lt;/li&gt;
&lt;li&gt;Consider using metaprogramming during class loading rather than runtime&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Code Readability
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Document why you're using metaprogramming&lt;/li&gt;
&lt;li&gt;Keep dynamic code generation simple and obvious&lt;/li&gt;
&lt;li&gt;Consider whether a more straightforward approach might work better&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ruby's evaluation methods provide powerful tools for metaprogramming, allowing you to write more dynamic and flexible code. While these tools should be used judiciously, understanding them opens up new possibilities for solving complex problems elegantly.&lt;/p&gt;

&lt;p&gt;Remember that with great power comes great responsibility – always consider whether metaprogramming is the best solution for your specific use case, and document your code well when you do use it.&lt;/p&gt;

&lt;p&gt;That's all for now. Thank you for reading it till the end. &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The GOAT Ruby on Rails 8 is here - What's new?</title>
      <dc:creator>Magesh</dc:creator>
      <pubDate>Sun, 05 Jan 2025 17:55:50 +0000</pubDate>
      <link>https://dev.to/railsfactory/the-goat-ruby-on-rails-8-is-here-whats-new-449h</link>
      <guid>https://dev.to/railsfactory/the-goat-ruby-on-rails-8-is-here-whats-new-449h</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2F8vhd5e7istwlkg9246w8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F8vhd5e7istwlkg9246w8.jpg" alt="Image description" width="800" height="574"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;DHH and the team recently launched Rails 8 in style at the Rails World Conference, like never before. Why? because this new version comes with a lot of features that are going to make it the God of all web frameworks&lt;/p&gt;

&lt;p&gt;We love how it simplifies the whole web development process helping you go from MVP to IPO in less time than you can imagine. You do not need a PAAS or any fancy cloud infrastructure anymore, you can build and deploy your product in a few seconds. More power to open-source.&lt;/p&gt;

&lt;p&gt;Isn't that fascinating? Let's look at how Rails 8 does that. &lt;/p&gt;

&lt;h2&gt;
  
  
  Unparalleled Performance
&lt;/h2&gt;

&lt;p&gt;Rails 8 includes enhancements to how ActiveRecord handles database queries, aiming for faster execution and reduced overhead. It has faster boot times for applications. Native support for multithreading and asynchronous processing and reduced memory consumption.&lt;/p&gt;

&lt;h2&gt;
  
  
  Zero-Downtime Deployment Made Simple - Kamal 2
&lt;/h2&gt;

&lt;p&gt;Kamal is an open-source zero-downtime deployment tool that simplifies server provisioning and running Rails applications on Docker containers. With Kamal, all you need is the IP address of your server or virtual machine (VM), and it handles everything—from installing development tools and dependencies to configuring your environment. It's that simple.&lt;/p&gt;

&lt;p&gt;Rails 8 now comes preconfigured with Kamal 2 and a highly optimized Dockerfile. This combination can transform a brand-new Linux server or VM into a fully functional application server with a single command: &lt;code&gt;kamal setup&lt;/code&gt;. Deployment has never been easier.&lt;/p&gt;

&lt;p&gt;Kamal also provides super-fast zero-downtime deploys, automated SSL certificates via Let’s Encrypt, and support for multiple applications on a single server without any complicated configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apache or Nginx not required anymore
&lt;/h2&gt;

&lt;p&gt;You no longer need Apache or Ngnix as a proxy server to work alongside your Rails Puma server. Introducing Thruster—a modern, lightweight proxy designed to sit in front of your Puma server.&lt;br&gt;
Thruster efficiently handles incoming internet traffic while providing features such as asset caching, compression, etc. &lt;/p&gt;

&lt;h2&gt;
  
  
  Solid Cable - Redis is no longer required
&lt;/h2&gt;

&lt;p&gt;You can now handle WebSocket functionality with just SQLite, eliminating the need for Redis or even a complex setup with MySQL or PostgreSQL. Solid Cable makes real-time messaging simple and accessible without relying on additional infrastructure. &lt;/p&gt;

&lt;h2&gt;
  
  
  Solid Cache - Memcache or Redis is no longer required
&lt;/h2&gt;

&lt;p&gt;If you want to fragment cache HTML for your rails application, you can do so with ease without the dependencies required before like memcache or redis service. Add a few lines of config and you can enable caching for your application. Caching no longer uses RAM storage instead it uses the disk which is cheaper. &lt;/p&gt;

&lt;h2&gt;
  
  
  Solid Queue - Background job frameworks are not needed
&lt;/h2&gt;

&lt;p&gt;Forget about integrating separate background job processing frameworks like Resque, Sidekiq, or Delayed Job. You can simply enable a solid queue in a few seconds which uses your existing database. Don't need Redis or any other dependencies for this purpose if you want to keep things simple. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Solid Queue can either run as a puma plugin, which is the default on a single-server installation, or by using the new bin/jobs command for starting a dedicated dispatcher.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is running on Production and is scalable. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Solid Queue has been meticulously developed within the pressures of a real production environment over the last 18 months, and today it’s running 20 million jobs per day for HEY alone at 37signals.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  No Build - Asset Management Simplified
&lt;/h2&gt;

&lt;p&gt;Propshaft is the new tool that bids goodbye to more than a decade-old Sprockets. The Javascript ecosystem has come a long way. We are at a time when we don't have to put up with the whole npm install and node-modules complexity. Now that all the major browsers support ES6 as default we no longer need to transcompile or other outdated processes. &lt;/p&gt;

&lt;p&gt;As the new asset pipeline, Propshaft only does 2 things now: Provide a load path for assets and stamp them with digests to allow for far-future expiry. Sprockets did a million other things that are no longer required if you want to keep things simple.&lt;/p&gt;

&lt;h2&gt;
  
  
  Generate your own authentication - Sorry, Devise!
&lt;/h2&gt;

&lt;p&gt;Most of the time people worry about using 3rd parties or plugins for authentication. Now, you can have your own authentication generated with a simple command. You have full control and it can be customized however you want. &lt;/p&gt;

&lt;p&gt;Just run &lt;code&gt;bin/rails generate authentication&lt;/code&gt; and you’ll get basic models for Session and User, together with a PasswordsMailer, SessionsController, and an Authentication concern&lt;/p&gt;

&lt;h2&gt;
  
  
  Want to know more?
&lt;/h2&gt;

&lt;h4&gt;
  
  
  ActiveRecord Enhancements:
&lt;/h4&gt;

&lt;p&gt;— Out-of-the-box support for JSONB queries, making it easier to handle structured data.&lt;br&gt;
— Optimized eager loading for associations to reduce query overhead.&lt;br&gt;
— Enhanced encryption for sensitive data, ensuring better security.&lt;/p&gt;

&lt;h4&gt;
  
  
  API improvements:
&lt;/h4&gt;

&lt;p&gt;— Improved lightweight API-only mode for faster and more efficient APIs.&lt;br&gt;
— Enhanced JSON serialization with optimized serializers for better performance.&lt;br&gt;
— Native support for GraphQL, making it easier to build powerful APIs.&lt;/p&gt;

&lt;h4&gt;
  
  
  Security Enhancements:
&lt;/h4&gt;

&lt;p&gt;— Automatic generation of Content Security Policies (CSP) for better protection against XSS attacks.&lt;br&gt;
— Strengthened CSRF protection for AJAX requests.&lt;br&gt;
— Default configuration for secure headers to improve application security.&lt;br&gt;
— Advanced management of encrypted credentials, ensuring sensitive data is secure.&lt;/p&gt;

&lt;h4&gt;
  
  
  Developer Experience Enhancements:
&lt;/h4&gt;

&lt;p&gt;— Improved error pages with actionable insights to debug issues faster.&lt;br&gt;
— Enhanced logging with structured logs for better debugging and monitoring.&lt;br&gt;
— A more interactive Rails console, improving developer productivity.&lt;br&gt;
— Simplified scaffolding with updated default templates to get started quickly.&lt;/p&gt;

&lt;h4&gt;
  
  
  What are you waiting for? Try Rails 8 Today!
&lt;/h4&gt;

&lt;p&gt;Rails on Rails 8 is packed with performance, security, and developer experience upgrades, making it the best choice for building modern applications. Whether you're starting a new project or considering &lt;a href="https://railsfactory.com/rails-upgrade-services/" rel="noopener noreferrer"&gt;upgrading your existing apps&lt;/a&gt;, Rails 8 is worth the move.&lt;/p&gt;

&lt;p&gt;Need help migrating your older apps to the latest version of Ruby on Rails? Contact &lt;a href="https://railsfactory.com/" rel="noopener noreferrer"&gt;RailsFactory&lt;/a&gt;—we’re here to make your transition smooth and efficient. Let’s take your apps to the next level together!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>webdev</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Write a SQL query</title>
      <dc:creator>Magesh</dc:creator>
      <pubDate>Tue, 16 Jul 2024 12:39:45 +0000</pubDate>
      <link>https://dev.to/magesh/write-a-sql-query-3g6j</link>
      <guid>https://dev.to/magesh/write-a-sql-query-3g6j</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fuhojj9lzfc2456im6me8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fuhojj9lzfc2456im6me8.jpg" alt="Image description" width="651" height="543"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Find the Total_Salary(Salary + Incentives) received by each employee&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Ruby is not dying — It’s Aging like fine wine</title>
      <dc:creator>Magesh</dc:creator>
      <pubDate>Mon, 25 Mar 2024 07:58:15 +0000</pubDate>
      <link>https://dev.to/magesh/ruby-is-not-dying-its-aging-like-fine-wine-jlb</link>
      <guid>https://dev.to/magesh/ruby-is-not-dying-its-aging-like-fine-wine-jlb</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2F26nx628zgo231zzfzbvr.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F26nx628zgo231zzfzbvr.jpeg" alt="Image description" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A few years ago, I noticed the growing popularity of JavaScript and felt that sticking solely to Ruby might not be sufficient. I decided to deepen my knowledge by exploring JavaScript, Node, React, Next.js, etc. I worked on a few projects and taught a lot of people through workshops.&lt;/p&gt;

&lt;p&gt;Fast forward to now, it’s been about five years, I haven’t written much Ruby code, and I’ve almost forgotten how simple it can be to write a piece of code. I’ve missed Ruby so much after seeing what the other side looks like, and now, I’m thinking of returning to the Ruby world (screw popularity and trends)&lt;/p&gt;

&lt;p&gt;Meanwhile, people have been asking if Ruby/Rails is dead, so here’s the reality:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ruby isn’t dead, it’s mature.&lt;/strong&gt; And like a seasoned professional, maturity brings its own set of strengths and values. Rails continues to be the outstanding prototyping framework it was 20 years ago with the release of new features like Hotwire which lets you accomplish more on the front-end without writing any Javascript code. That’s Rails magic.&lt;/p&gt;

&lt;p&gt;Ruby is not trending anymore but that doesn’t mean the language is about to die. If anything, it is aging like fine wine. Developers might jump ship based on changing trends but Ruby will live because of its powerful community and people like us who continue to use it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fa9e2liza3viwcjqboepq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fa9e2liza3viwcjqboepq.png" alt="Image description" width="800" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enough talk, Let’s look at some key data points from the &lt;a href="https://survey.stackoverflow.co/2023/" rel="noopener noreferrer"&gt;Stack Overflow 2023 Developer Survey&lt;/a&gt; that showcase Ruby’s continued relevance and potential:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Popularity and Usage:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Overall Usage:&lt;/strong&gt;&lt;br&gt;
 While slightly down to 6.23% compared to 6.05% in 2022, Ruby holds the 16th position among the most used technologies (it’s above Dart, Scala, Swift, and Elixir in ranking). This indicates its continued presence in the developer landscape.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Professional Usage:&lt;/strong&gt;&lt;br&gt;
 Among professional developers, Ruby usage is 6.94% in 2023, compared to 6.72% in 2022, showing a slight upward trend. This suggests continued adoption and demand for Ruby skills in the professional job market.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Learning:&lt;/strong&gt;&lt;br&gt;
 2.55% of respondents are interested in learning Ruby, similar to the previous year (2.52%). This suggests a steady interest in the language, particularly among new developers even though it is low compared to other languages like Python and Javascript. Attracting new talent to the Ruby ecosystem might require some focused efforts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Developer Satisfaction and Experience:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Satisfaction:&lt;/strong&gt;&lt;br&gt;
 47.69% of developers who use Ruby admire it, indicating a positive sentiment towards the language and its capabilities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Years of Experience:&lt;/strong&gt;&lt;br&gt;
 Ruby developers have an average of nearly 12 years of experience, highlighting a mature and experienced user base. This often translates to greater skills and expertise within the community.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Other Interesting Points:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Foq98ztafrduvjem9sjb7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Foq98ztafrduvjem9sjb7.png" alt="Image description" width="800" height="703"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Pay:&lt;/strong&gt;&lt;br&gt;
 Interestingly, professional Ruby developers report a higher median salary than Python, Go, Scala, or Java developers (That is a surprise). According to the survey, the median salary for professional Ruby developers is $98,522 which has increased compared to the previous year ($93,000). This suggests that Ruby skills can be financially rewarding for experienced developers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Web Development Usage:&lt;/strong&gt;&lt;br&gt;
 While Ruby on Rails, the popular web framework built on Ruby, saw a slight dip to 5.49% usage in 2023 from 5.83% in 2022, it still ranks among the top web frameworks. This indicates its continued relevance in web development, even if it is not experiencing explosive growth.&lt;/p&gt;

&lt;p&gt;In conclusion, the Stack Overflow Developer Survey data paints a nuanced picture of Ruby. While it may not be the most popular language, it still holds relevance and value for developers, particularly those seeking a stable, mature, and rewarding career path.&lt;/p&gt;

&lt;p&gt;Its strong community, rich ecosystem, and potential for higher compensation make it a language worth considering for both seasoned developers and those embarking on their development careers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Maturity and stability:&lt;/strong&gt; Ruby and Rails have been around for over 25 years, giving them a strong foundation and a proven stability and reliability track record. This can be attractive for projects requiring long-term maintenance and scalability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flexibility and adaptability:&lt;/strong&gt; Ruby’s dynamic nature and extensive library of gems allow developers to build a wide range of applications without getting bogged down in complex frameworks or configurations.&lt;/p&gt;

&lt;p&gt;Remember, while trends may shift, the value of a language often lies in its ability to solve problems effectively and efficiently. Ruby, with its unique strengths and dedicated community, continues to be a valuable tool in the developer’s toolbox.&lt;/p&gt;

&lt;p&gt;There are always exciting developments in Ruby and Rails every once in a while. I’m eagerly waiting to see more interesting features and can’t wait to start my next project in Rails with Hotwire. I also want to explore Turbo Native and Strada. See? Many new things.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fgonuklfk652lyncv83kz.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fgonuklfk652lyncv83kz.jpeg" alt="Image description" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To sum it all up, the evening will feature a variety of cocktails, beers, and even tequila shots, but wine enthusiasts will remain steadfast. Do you catch my drift?&lt;/p&gt;

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

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
