<?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: Chaitali Khangar</title>
    <description>The latest articles on DEV Community by Chaitali Khangar (@chaitalikhangar).</description>
    <link>https://dev.to/chaitalikhangar</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%2F831503%2F49f224a9-11b1-4f9e-9188-c236a2070cc4.jpeg</url>
      <title>DEV Community: Chaitali Khangar</title>
      <link>https://dev.to/chaitalikhangar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chaitalikhangar"/>
    <language>en</language>
    <item>
      <title>The Factory Method Pattern: The Secret Behind Scalable Code</title>
      <dc:creator>Chaitali Khangar</dc:creator>
      <pubDate>Wed, 12 Nov 2025 15:52:39 +0000</pubDate>
      <link>https://dev.to/chaitalikhangar/the-factory-method-pattern-the-secret-behind-scalable-code-2fjc</link>
      <guid>https://dev.to/chaitalikhangar/the-factory-method-pattern-the-secret-behind-scalable-code-2fjc</guid>
      <description>&lt;p&gt;A few years ago, I worked on a growing application that started small but expanded faster than we expected.&lt;/p&gt;

&lt;p&gt;Each new feature introduced a new way to create objects - notifications, reports, services - and every time, we added another if-else block.&lt;/p&gt;

&lt;p&gt;At first, it was fine.&lt;/p&gt;

&lt;p&gt;Then one day, a minor change broke three modules because a single creation method was edited in multiple files.&lt;/p&gt;

&lt;p&gt;That's when I realized the problem wasn't in the logic - it was in the design.&lt;/p&gt;

&lt;p&gt;We weren't managing object creation; we were repeating it.&lt;br&gt;
That's when I discovered design patterns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Design Patterns?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As applications evolve, developers often face the same structural problems repeatedly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How do you create objects without duplicating logic?&lt;br&gt;
How do you extend functionality without breaking existing code?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead of solving these problems from scratch every time, developers started identifying and documenting reusable approaches.&lt;/p&gt;

&lt;p&gt;That led to the Gang of Four (GoF) book - Design Patterns: Elements of Reusable Object-Oriented Software (1994).&lt;/p&gt;

&lt;p&gt;A design pattern isn't a library or a framework.&lt;br&gt;
It's a reusable way to think about recurring design challenges.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Real Problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine you're building a notification service.&lt;br&gt;
You start with a simple design:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if type == :email
  Notification::Email.new
elsif type == :sms
  Notification::Sms.new
else
  Notification::Push.new
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works.&lt;/p&gt;

&lt;p&gt;But as the system grows, more notification types appear - Slack, WhatsApp, internal alerts.&lt;/p&gt;

&lt;p&gt;Each new type means another line, another edit, another potential mistake.&lt;/p&gt;

&lt;p&gt;Your code is now doing two things:&lt;br&gt;
Running business logic.&lt;br&gt;
Deciding which class to create.&lt;/p&gt;

&lt;p&gt;This is exactly the kind of problem the Factory Method Pattern solves.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What Is the Factory Method Pattern?&lt;/strong&gt;&lt;br&gt;
The Factory Method Pattern is a creational design pattern that provides an interface for creating objects without specifying their concrete classes.&lt;/p&gt;

&lt;p&gt;In other words, you tell the system what you need, and it decides how to build it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How It Works&lt;/strong&gt;&lt;br&gt;
Let's refactor the earlier example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class NotificationFactory
  def self.build(type)
    klass_name = "Notifications::#{type.to_s.camelize}"

    if Object.const_defined?(klass_name)
      klass_name.constantize.new
    else
      raise "Unknown notification type: #{type}"
    end
  end
end
module Notification
  class Email
    def send_message
      puts "Sending email..."
    end
  end

  class Sms
    def send_message
      puts "Sending SMS..."
    end
  end

  class Push
    def send_message
      puts "Sending push notification..."
    end
  end
end

# When you call
notification = NotificationFactory.build(:email)
notification.send_message
# Output: Sending email...

notification = NotificationFactory.build(:whatsapp)
notification.send_message
# Output: Unknown notification type: whatsapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you have implemented a Factory Method pattern.&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%2Fgjkcf119enifbq8khuz4.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%2Fgjkcf119enifbq8khuz4.png" alt=" " width="800" height="1200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the future, if a new type Slack is added,&lt;br&gt;
You just define the class inside Notification- no change in the factory, no code broken.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Visual Flow&lt;/strong&gt;&lt;br&gt;
Here's how this example works conceptually:&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%2Frc79sfg9z0pa8rqs64fm.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%2Frc79sfg9z0pa8rqs64fm.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the essence of the Factory Method Pattern - &lt;br&gt;
The client doesn't care which class was created.&lt;br&gt;
It just asks the factory, and the right object is returned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How Major Frameworks Use It&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Factory Method Pattern is everywhere - even if you don't notice it.&lt;br&gt;
&lt;strong&gt;Rails:&lt;/strong&gt; &lt;br&gt;
When you call validates :email, presence: true, Rails dynamically decides whether to use PresenceValidator, LengthValidator, or FormatValidator.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React:&lt;/strong&gt;&lt;br&gt;
React.createElement(type) determines which component to render at runtime.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When (and When Not) to Use the Factory Method Pattern&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before applying it, here's a quick decision framework to help you decide whether the Factory Method Pattern is the right choice.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Use the Factory Method Pattern when:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You have multiple related object types.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You expect to add more types in the future.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You want to centralize and simplify object creation logic.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Avoid it when:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The number of types is fixed and small.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Simplicity matters more than scalability.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now you've seen how replacing scattered if-else statements with intentional creation logic brings clarity and structure.&lt;/p&gt;

&lt;p&gt;You're not just improving readability - you're building for the future.&lt;/p&gt;

&lt;p&gt;Till next time - build systems that grow, but never crumble.&lt;/p&gt;

&lt;h1&gt;
  
  
  ruby #rails #designpatterns #factorymethod
&lt;/h1&gt;

</description>
      <category>architecture</category>
      <category>designpatterns</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Dynamic Data Management with jsonb in Rails</title>
      <dc:creator>Chaitali Khangar</dc:creator>
      <pubDate>Tue, 16 Apr 2024 11:42:46 +0000</pubDate>
      <link>https://dev.to/chaitalikhangar/dynamic-data-management-with-jsonb-in-rails-3h7m</link>
      <guid>https://dev.to/chaitalikhangar/dynamic-data-management-with-jsonb-in-rails-3h7m</guid>
      <description>&lt;p&gt;Ever feel stuck trying to manage data fields that constantly evolve in your application?&lt;/p&gt;

&lt;p&gt;You're certainly not alone. &lt;/p&gt;

&lt;p&gt;Many developers face this challenge when building applications that require flexible data structures.&lt;/p&gt;

&lt;p&gt;But there's a solution! &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PostgreSQL's jsonb&lt;/strong&gt; data type is here to help. &lt;/p&gt;

&lt;p&gt;Let's explore how jsonb helps you to handle dynamic data with ease.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;What is jsonb?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It's a data type designed specifically to store flexible and dynamic data in the structured format of JSON (JavaScript Object Notation). &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Think of it as a way to store key-value pairs within your database table, allowing you to define data as your application's needs grow.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"product_properties": {
  "color": "blue",
  "price": 19.99
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, product_properties is a jsonb column that stores product details like color and price. You can effortlessly add new properties (like size or material) without modifying your database schema.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Why jsonb over json?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Jsonb stores data in a binary format, which results in more efficient storage and faster retrieval.&lt;/li&gt;
&lt;li&gt;Jsonb support indexing&lt;/li&gt;
&lt;li&gt;Jsonb provides a larger set of functions and operators for querying and manipulating JSON data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;When to use jsonb?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Flexible Schemas: Ditch rigid table structures! jsonb allows your data to adapt as your application's needs change.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dynamic Attributes: Need to store user preferences or custom product information? jsonb handles it beautifully.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;How to use jsonb?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's a quick three-step guide to using jsonb in your Rails project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step1&lt;/strong&gt;: Create a Migration
Add a jsonb column to your table using a migration. Here's an example using Rails:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;add_column :products, :properties, :jsonb, null: false, default: '{}'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step2&lt;/strong&gt;: Setting Values
Treat your jsonb column just like any other object. Assign values using key-value pairs:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;product = Product.first
product.properties = { 'color': 'blue', 'price': 19.99 }
product.save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step3&lt;/strong&gt;: Accessing Values
Retrieve data based on keys or values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;SQL way&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select * from products where (products.properties-&amp;gt;&amp;gt;'color')::varchar = 'blue';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Rails Way&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product.where("(products.properties-&amp;gt;&amp;gt;'color')::varchar = 'blue'")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Remember:&lt;/strong&gt; jsonb values can be null. &lt;/p&gt;

&lt;p&gt;Use the following to check for existing keys:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;SQL way&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select * from products where (products.properties-&amp;gt;&amp;gt;'color') is not NULL;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Rails Way&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product.where("products.properties-&amp;gt;&amp;gt;'color' is not NULL")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Embrace the Power of jsonb!&lt;/p&gt;

&lt;p&gt;With jsonb you can say goodbye to those frustrating moments when dealing with dynamic data in your PostgreSQL database. &lt;/p&gt;

&lt;p&gt;Now you can store and retrieve information in a structured and flexible way. This gives your application the agility to adapt and grow as your needs evolve.&lt;/p&gt;

&lt;p&gt;Till we meet, next time Happy Coding!!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>postgres</category>
      <category>jsonb</category>
      <category>ruby</category>
    </item>
    <item>
      <title>The Art of Monkey Patching in Ruby</title>
      <dc:creator>Chaitali Khangar</dc:creator>
      <pubDate>Tue, 26 Mar 2024 05:54:25 +0000</pubDate>
      <link>https://dev.to/chaitalikhangar/the-art-of-monkey-patching-in-ruby-537k</link>
      <guid>https://dev.to/chaitalikhangar/the-art-of-monkey-patching-in-ruby-537k</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbozujn690krk3j51by2o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbozujn690krk3j51by2o.png" alt="Image description" width="800" height="466"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ruby, celebrated for its dynamic nature and Metaprogramming capabilities, offers a wealth of powerful features that are often underutilized or misunderstood.&lt;/p&gt;

&lt;p&gt;In this blog, we'll shed light on a fascinating topic within Ruby known as Monkey Patching.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;We are going to cover:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is Monkey Patching?&lt;/li&gt;
&lt;li&gt;How can we apply Monkey Patching?&lt;/li&gt;
&lt;li&gt;What happens when Monkey Patching goes wrong?&lt;/li&gt;
&lt;li&gt;Is there an alternative approach?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's wear our Ruby hats and start diving into the concept of Monkey Patching.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;What is Monkey Patching?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ruby is a dynamic programming language and its interpreted language which gives you the ability to write or modify the code at Runtime.&lt;/p&gt;

&lt;p&gt;Imagine you could add new abilities to existing things in Ruby. &lt;/p&gt;

&lt;p&gt;That's what open classes or Monkey Patching. &lt;/p&gt;

&lt;p&gt;Monkey Patch, lets you change or add things to existing stuff, like teaching a phone new tricks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Why do we use it?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you want to make something better without changing the original code.&lt;/p&gt;

&lt;p&gt;How can we apply Monkey Patching?&lt;/p&gt;

&lt;p&gt;Now we know what it is, let's dive into coding.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require 'date'

# TO check if date class has leap_year? method or not
p Date.methods.include?(:leap_year?)
# Output: false

class Date
  def leap_year?
    (year % 4 == 0 &amp;amp;&amp;amp; year % 100 != 0) || (year % 400 == 0)
  end
end

date = Date.new(2023, 10, 29)
p date.leap_year?
# Output: false

date = Date.new(2024, 2, 29)
p date.leap_year?
# Output: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we open the &lt;code&gt;Date&lt;/code&gt; class, that's why it is called an &lt;strong&gt;Open Class&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;You are adding a new &lt;code&gt;leap_year?&lt;/code&gt; method here that is called &lt;strong&gt;Monkey Patching&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;What happens when Monkey Patching goes wrong?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While open classes are powerful, using them too much or in the wrong way can make things confusing or unpredictable.&lt;/p&gt;

&lt;p&gt;Don't change the main things without thinking, as it might make others confused or stop things from working together.&lt;/p&gt;

&lt;p&gt;Now let's understand this by example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Check if a method exists
String.methods.include?(:to_alphanumeric)

# Redefine class
class String
  def to_alphanumeric
    gsub(/[^\w\s]/, '')
  end
end

p "Hello, @World!".to_alphanumeric
# Output: Hello World

p "Ruby is #1 for coding!".to_alphanumeric
# Output: Ruby is 1 for coding
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now whenever you are calling the &lt;code&gt;to_alphanumeric&lt;/code&gt; method on the &lt;strong&gt;String&lt;/strong&gt; class you will get the same result.&lt;/p&gt;

&lt;p&gt;This changes the global definition of the &lt;code&gt;to_alphanumeric&lt;/code&gt; method of the String class.&lt;/p&gt;

&lt;p&gt;Now consider third-party applications/gems if they have the same method &lt;code&gt;to_alphanumeric&lt;/code&gt; for the &lt;strong&gt;String&lt;/strong&gt; class, then it will behave differently, it will use your code instead of executing third-party applications or gem code.&lt;/p&gt;

&lt;p&gt;We don't want this, to impact globally. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;How can we solve this?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Is there an alternative approach?&lt;/p&gt;

&lt;p&gt;Ruby 2.0 came up with a great feature to solve this issue. It's called &lt;strong&gt;Refinement&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;What exactly is 'Refinement' all about?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It allows you to modify the behavior of a class or module temporarily while ensuring it doesn't impact the global definition.&lt;/p&gt;

&lt;p&gt;Provides a way to extend a class locally.&lt;/p&gt;

&lt;p&gt;Refinements can modify both classes and modules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;How can we implement the Refinement feature?&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module ToJSON
  refine Hash do
    def to_json
      '{' + map { |k, v| k.to_s.dump + ':' + v.to_s.dump }.join(',') + '}'
    end
   end
end

hash = {1=&amp;gt;2}
p hash.to_json

# Output: undefined method `to_json' for {1=&amp;gt;2}:Hash (NoMethodError)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oops, an error is thrown.&lt;/p&gt;

&lt;p&gt;What's the reason behind the error? The reason is it does not modify the global definition for the Hash class.&lt;/p&gt;

&lt;p&gt;But hold on, we need these changes for the 'Hash' class, to fix this we must utilize the 'using' keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module ToJSON
  refine Hash do
    def to_json
      '{' + map { |k, v| k.to_s.dump + ':' + v.to_s.dump }.join(',') + '}'
    end
   end
end

module ConvertToJson
  using ToJSON
  def self.convert(hash)
    p hash.to_json
  end
end

hash = {1=&amp;gt;2}
ConvertToJson.convert(hash)
# Output: "{\"1\":\"2\"}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yay, we've accomplished this without affecting the global definition of Hash.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's discuss the Scope of Refinement:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's in the refine block itself.&lt;/li&gt;
&lt;li&gt;Code starts from the place where you call using until the end of the module.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now you understand how Monkey Patching works, why Refinement is introduced, and how you can use it. &lt;/p&gt;

&lt;p&gt;Refactor your Monkey Patches or Refinements, and review your code to ensure they still serve your project's goals.&lt;/p&gt;

&lt;p&gt;If you want to use it, go ahead as now you know how you can use Refinement to prevent global scope.&lt;/p&gt;

&lt;p&gt;Till we meet next time, Happy Coding!!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>Colorful Text in Rails with ANSI Escape Codes</title>
      <dc:creator>Chaitali Khangar</dc:creator>
      <pubDate>Mon, 25 Mar 2024 08:10:07 +0000</pubDate>
      <link>https://dev.to/chaitalikhangar/colorful-text-in-rails-with-ansi-escape-codes-2ki4</link>
      <guid>https://dev.to/chaitalikhangar/colorful-text-in-rails-with-ansi-escape-codes-2ki4</guid>
      <description>&lt;p&gt;&lt;em&gt;&lt;strong&gt;Ever wonder about how Rails produces those vibrant, colorful displays for RSpec and Logger?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Take a look at this piece of Ruby code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;puts "\e[32mHi"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧐 'Hi' appears in dazzling green.&lt;/p&gt;

&lt;p&gt;Here's the magic behind it: ANSI escape codes.&lt;/p&gt;

&lt;p&gt;These codes were cleverly designed in programming languages to provide developers with a standardized way to control the format and appearance of text in terminal/console output.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;When do we use this?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;1️⃣ Enhanced Readability: ANSI escape codes make text more readable by allowing developers to change colors, styles, and backgrounds, ensuring that important information stands out.&lt;/p&gt;

&lt;p&gt;2️⃣ Visual Feedback: These mystical codes offer visual cues to users, from progress bars to color-coded status updates.&lt;/p&gt;

&lt;p&gt;3️⃣ Debugging and Logging: They help format debugging and logging messages, enabling developers to quickly identify the severity of issues based on text appearance.&lt;/p&gt;

&lt;p&gt;4️⃣ Interactive Interfaces: ANSI escape codes make command-line applications more interesting by allowing them to create interactive user interfaces.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Where does Rails work its magic with these codes?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;RSpec&lt;/li&gt;
&lt;li&gt;Logger's Colorful Logs&lt;/li&gt;
&lt;li&gt;Colorful Console Output&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Here is the bonus:&lt;/p&gt;

&lt;p&gt;If you want to check where ANSI code exists in Rails feel free to reach out:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/rails/rails/blob/6c9967f85ada90f8ca58e794a850e887ceb45132/activesupport/lib/active_support/log_subscriber.rb#L76"&gt;https://github.com/rails/rails/blob/6c9967f85ada90f8ca58e794a850e887ceb45132/activesupport/lib/active_support/log_subscriber.rb#L76&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/rspec/rspec-core/blob/1c662c35f91f33946fdfbed431878f4798f06f70/lib/rspec/core/formatters/console_codes.rb#L47"&gt;https://github.com/rspec/rspec-core/blob/1c662c35f91f33946fdfbed431878f4798f06f70/lib/rspec/core/formatters/console_codes.rb#L47&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Till we meet next time. Happy Coding!!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>The Curious Case of puts: A Ruby Detective Story</title>
      <dc:creator>Chaitali Khangar</dc:creator>
      <pubDate>Thu, 21 Mar 2024 14:10:19 +0000</pubDate>
      <link>https://dev.to/chaitalikhangar/the-curious-case-of-puts-a-ruby-detective-story-34o7</link>
      <guid>https://dev.to/chaitalikhangar/the-curious-case-of-puts-a-ruby-detective-story-34o7</guid>
      <description>&lt;p&gt;Ever wonder where the puts method comes from in Ruby?&lt;/p&gt;

&lt;p&gt;Let's check out that together.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;puts.class # =&amp;gt; NilClass
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  It returns NilClass. But why?
&lt;/h2&gt;

&lt;p&gt;It's because calling puts without arguments returns nil, signifying the absence of a specific value. &lt;/p&gt;

&lt;p&gt;NilClass represents this very concept – the lack of a defined value.&lt;/p&gt;

&lt;p&gt;Now, to unravel the true origin of puts, we will execute this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;puts_method = method(:puts)
puts puts_method.owner
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code reveals that puts originates from the Kernel.&lt;/p&gt;

&lt;p&gt;Let's check out whether Kernel is a class or a Module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Kernel.is_a? Class #=&amp;gt; false
Kernel.is_a? Module #=&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This clarifies that Kernel is a module.&lt;/p&gt;

&lt;h2&gt;
  
  
  But, how do puts magically work without a receiver?
&lt;/h2&gt;

&lt;p&gt;In Ruby, the "Object" class includes the Kernel module. &lt;/p&gt;

&lt;p&gt;This makes Kernel an essential part of every object's ancestry.&lt;/p&gt;

&lt;p&gt;As Ruby code runs within an object, you can call Kernel's instance methods from anywhere in your code.&lt;/p&gt;

&lt;p&gt;So, next time you use 'puts' in your Ruby code, remember its origins in the Kernel module and how it seamlessly integrates into every object's ancestry, making it accessible from anywhere in your code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbr7kq2kub4ovz2e4kmxw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbr7kq2kub4ovz2e4kmxw.jpeg" alt="Image description" width="800" height="373"&gt;&lt;/a&gt;Till we meet, Happy Coding!!&lt;/p&gt;

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