<?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: Anand Soni</title>
    <description>The latest articles on DEV Community by Anand Soni (@sonianand11).</description>
    <link>https://dev.to/sonianand11</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%2F270978%2F65eb329b-7be8-4f04-a2c2-9a865c95f1d6.png</url>
      <title>DEV Community: Anand Soni</title>
      <link>https://dev.to/sonianand11</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sonianand11"/>
    <language>en</language>
    <item>
      <title>Writing Clean Code in Ruby on Rails Applications 🧼💻</title>
      <dc:creator>Anand Soni</dc:creator>
      <pubDate>Thu, 07 Nov 2024 18:10:18 +0000</pubDate>
      <link>https://dev.to/sonianand11/writing-clean-code-in-ruby-on-rails-applications-85h</link>
      <guid>https://dev.to/sonianand11/writing-clean-code-in-ruby-on-rails-applications-85h</guid>
      <description>&lt;p&gt;Building robust and maintainable applications with Ruby on Rails is all about writing clean, organized code. Clean code is easy to read, understand, and adapt. In Rails applications, clean code principles help us navigate between models, controllers, views, and other components seamlessly. Here’s how to achieve that clean, well-structured code in a Rails app.&lt;/p&gt;




&lt;h4&gt;
  
  
  1. Use Descriptive Naming 🎯
&lt;/h4&gt;

&lt;p&gt;Good names are crucial for readability. In Rails:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use descriptive method and variable names. For example, &lt;code&gt;calculate_total&lt;/code&gt; is clearer than &lt;code&gt;calc&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Stick to Rails conventions: &lt;code&gt;user_profile_path&lt;/code&gt; for URLs or &lt;code&gt;user_profile&lt;/code&gt; for methods related to users.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Naming conventions are a roadmap for anyone reading the code.&lt;/p&gt;




&lt;h4&gt;
  
  
  2. Keep Methods Small and Focused 🔍
&lt;/h4&gt;

&lt;p&gt;Avoid long methods that try to do too much. Break complex logic down into smaller methods that handle one responsibility. This helps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increase readability.&lt;/li&gt;
&lt;li&gt;Simplify debugging.&lt;/li&gt;
&lt;li&gt;Allow for reusability.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Rails controllers, keep actions like &lt;code&gt;create&lt;/code&gt;, &lt;code&gt;show&lt;/code&gt;, and &lt;code&gt;index&lt;/code&gt; simple. Complex logic should be offloaded to service objects or model methods.&lt;/p&gt;




&lt;h4&gt;
  
  
  3. Avoid Hardcoding — Use Constants &amp;amp; ENV Variables 🔒
&lt;/h4&gt;

&lt;p&gt;Hardcoding values can lead to maintenance headaches. Instead:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use environment variables for sensitive data, like API keys.&lt;/li&gt;
&lt;li&gt;Define constants for values that rarely change.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, rather than embedding a URL directly in code, place it in &lt;code&gt;ENV['API_URL']&lt;/code&gt;. This approach keeps things secure and adaptable.&lt;/p&gt;




&lt;h4&gt;
  
  
  4. Keep Your Controllers Slim 👀
&lt;/h4&gt;

&lt;p&gt;Controllers are the middlemen between models and views; they shouldn’t be loaded with business logic. Use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Service objects&lt;/strong&gt; for complicated workflows or heavy logic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concerns&lt;/strong&gt; to share code between controllers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Before_actions&lt;/strong&gt; sparingly for authentication and repetitive tasks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By keeping your controllers lean, they’re easier to read and maintain.&lt;/p&gt;




&lt;h4&gt;
  
  
  5. Use Scopes in Models 📏
&lt;/h4&gt;

&lt;p&gt;Rails models can get unwieldy if they handle too many responsibilities. Scopes help filter data cleanly. For instance, instead of repeatedly querying &lt;code&gt;where(status: "active")&lt;/code&gt;, define a scope:&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;scope&lt;/span&gt; &lt;span class="ss"&gt;:active&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;status: &lt;/span&gt;&lt;span class="s1"&gt;'active'&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;This makes your code more readable and removes duplication.&lt;/p&gt;




&lt;h4&gt;
  
  
  6. Follow DRY (Don’t Repeat Yourself) 🧑‍💻
&lt;/h4&gt;

&lt;p&gt;Redundancy bloats your codebase. Rails provides ways to keep things DRY:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use partials in views for reusable components.&lt;/li&gt;
&lt;li&gt;Extract repeated logic into helper methods, concerns, or service objects.&lt;/li&gt;
&lt;li&gt;Embrace Rails built-ins like &lt;code&gt;before_action&lt;/code&gt; to eliminate repetitive code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;DRY code is not only efficient but easier to change when needed.&lt;/p&gt;




&lt;h4&gt;
  
  
  7. Write Tests Alongside Your Code 🧪
&lt;/h4&gt;

&lt;p&gt;Testing is crucial in Rails development, and clean code benefits from it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write &lt;strong&gt;RSpec&lt;/strong&gt; tests for models, controllers, and views.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;factories&lt;/strong&gt; to simplify test setup.&lt;/li&gt;
&lt;li&gt;Embrace &lt;strong&gt;Test-Driven Development (TDD)&lt;/strong&gt;: it keeps your code concise and helps detect issues early.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tests are a safety net for maintaining clean code.&lt;/p&gt;




&lt;h4&gt;
  
  
  8. Document Your Code Where Needed 📝
&lt;/h4&gt;

&lt;p&gt;While clean code should be self-explanatory, some parts may need additional context. In those cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use comments to clarify complex logic.&lt;/li&gt;
&lt;li&gt;Avoid excessive commenting—only add comments where necessary.&lt;/li&gt;
&lt;li&gt;For methods that are public-facing, consider &lt;strong&gt;YARD&lt;/strong&gt; documentation to keep things organized.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Comments should add value, not clutter.&lt;/p&gt;




&lt;h4&gt;
  
  
  9. Avoid N+1 Queries with Eager Loading 🚀
&lt;/h4&gt;

&lt;p&gt;Performance is part of clean code. In Rails, avoid N+1 queries by using &lt;strong&gt;eager loading&lt;/strong&gt; for associations:&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;# Bad: This causes an N+1 query issue&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;all&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;user&lt;/span&gt;&lt;span class="o"&gt;|&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;profile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# Good: Eager loading solves this&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;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:profile&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;user&lt;/span&gt;&lt;span class="o"&gt;|&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;profile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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;Eager loading improves performance and keeps your database calls efficient.&lt;/p&gt;




&lt;h4&gt;
  
  
  10. Leverage Rails’ Built-In Helpers 🧰
&lt;/h4&gt;

&lt;p&gt;Rails is packed with built-in methods and helpers that simplify common tasks. Use these to keep your code concise and clean:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;link_to&lt;/code&gt;, &lt;code&gt;button_to&lt;/code&gt;, etc., for links and buttons in views.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;number_to_currency&lt;/code&gt; for formatting currency.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;pluck&lt;/code&gt; instead of &lt;code&gt;map&lt;/code&gt; for database fields to save on memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Rails’ helpers are designed to keep your code elegant and concise.&lt;/p&gt;




&lt;h4&gt;
  
  
  Conclusion 🌟
&lt;/h4&gt;

&lt;p&gt;Clean code is the foundation of a maintainable, efficient Ruby on Rails application. By sticking to descriptive naming, avoiding duplication, keeping controllers slim, and using Rails’ built-in features, your code remains clear and adaptable. Investing in clean code principles not only improves your Rails app today but ensures that future developers will have an easier time maintaining and extending it. 🚀&lt;/p&gt;




&lt;p&gt;Clean code in Rails isn’t just about writing less code; it’s about writing code that communicates its purpose and function clearly. Happy coding! 🎉&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>powerfuldevs</category>
      <category>programming</category>
    </item>
    <item>
      <title>The Importance of Test-Driven Development (TDD) and What Companies Expect from Candidates</title>
      <dc:creator>Anand Soni</dc:creator>
      <pubDate>Tue, 15 Oct 2024 16:47:02 +0000</pubDate>
      <link>https://dev.to/sonianand11/the-importance-of-test-driven-development-tdd-and-what-companies-expect-from-candidates-360n</link>
      <guid>https://dev.to/sonianand11/the-importance-of-test-driven-development-tdd-and-what-companies-expect-from-candidates-360n</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7zmv4htsjklend0muuvb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7zmv4htsjklend0muuvb.jpg" alt="Test Driven Development TDD" width="625" height="370"&gt;&lt;/a&gt;&lt;strong&gt;Test-Driven Development (TDD)&lt;/strong&gt; is a software practice where you write tests &lt;strong&gt;before&lt;/strong&gt; the code itself. It follows a clear cycle known as &lt;strong&gt;Red-Green-Refactor&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Red&lt;/strong&gt;: Write a test that fails (🔴).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Green&lt;/strong&gt;: Write just enough code to make the test pass (🟢).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Refactor&lt;/strong&gt;: Improve and clean the code while keeping the test passing (🔄).&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Why TDD Matters 🚀
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. &lt;strong&gt;Improved Code Quality&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;By writing tests first, you ensure the code behaves exactly as expected, leading to &lt;strong&gt;fewer bugs&lt;/strong&gt; and more reliable software. Tests act as a safety net as the code evolves. 🛡️&lt;/p&gt;

&lt;h4&gt;
  
  
  2. &lt;strong&gt;Faster Feedback Loops&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;When a test fails, you get immediate feedback, making it easier to find and fix issues &lt;strong&gt;early&lt;/strong&gt; in development. This minimizes long debugging sessions later. 🕒&lt;/p&gt;

&lt;h4&gt;
  
  
  3. &lt;strong&gt;Modular Design&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;TDD encourages breaking down complex features into &lt;strong&gt;small, manageable units&lt;/strong&gt;. This leads to better, more maintainable designs, where each function does one thing well. 🧩&lt;/p&gt;

&lt;h4&gt;
  
  
  4. &lt;strong&gt;Clear Communication and Collaboration&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Tests serve as &lt;strong&gt;living documentation&lt;/strong&gt; for the code. This helps other developers, testers, or stakeholders understand what the code is supposed to do. 📝&lt;/p&gt;

&lt;h4&gt;
  
  
  5. &lt;strong&gt;Reduced Maintenance Costs&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;A stable codebase, backed by tests, is much easier to maintain. Changes are safer, and you can extend functionality without fear of breaking existing features. 🛠️&lt;/p&gt;

&lt;h4&gt;
  
  
  6. &lt;strong&gt;Fit for Agile/DevOps&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;TDD works well in &lt;strong&gt;Agile&lt;/strong&gt; and &lt;strong&gt;DevOps&lt;/strong&gt; environments, where continuous integration (CI) and delivery are crucial. It ensures code is always in a deployable state. ⚡&lt;/p&gt;




&lt;h3&gt;
  
  
  What Companies Look for in TDD Candidates 👀
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. &lt;strong&gt;Testing Framework Proficiency&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Companies want candidates who are comfortable using tools like &lt;strong&gt;RSpec&lt;/strong&gt; (Ruby), &lt;strong&gt;JUnit&lt;/strong&gt; (Java), &lt;strong&gt;PyTest&lt;/strong&gt; (Python), or &lt;strong&gt;Jest&lt;/strong&gt; (JavaScript) to write tests that cover key scenarios. ⚙️&lt;/p&gt;

&lt;h4&gt;
  
  
  2. &lt;strong&gt;Experience with TDD Cycle&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Being able to &lt;strong&gt;demonstrate&lt;/strong&gt; experience with the Red-Green-Refactor process is crucial. Explaining how you’ve used it in real projects makes a strong impression. 💡&lt;/p&gt;

&lt;h4&gt;
  
  
  3. &lt;strong&gt;Clean Code and Design Patterns&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;TDD naturally leads to cleaner, more modular code. Companies value developers who write &lt;strong&gt;DRY&lt;/strong&gt; (Don't Repeat Yourself) code and follow solid design principles. 🧼&lt;/p&gt;

&lt;h4&gt;
  
  
  4. &lt;strong&gt;Collaborative Team Player&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Since TDD is often part of team-based workflows, being able to &lt;strong&gt;communicate and collaborate&lt;/strong&gt; effectively is essential. Companies look for developers who can align with QA teams and product managers. 🤝&lt;/p&gt;

&lt;h4&gt;
  
  
  5. &lt;strong&gt;Problem-Solving Skills&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Companies want candidates who can write tests that catch edge cases and prevent future issues from slipping into production. They want problem solvers who think ahead. 🔍&lt;/p&gt;

&lt;h4&gt;
  
  
  6. &lt;strong&gt;Continuous Learning&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;With frameworks and tools constantly evolving, companies expect developers to &lt;strong&gt;stay up-to-date&lt;/strong&gt; on best practices and continually improve their TDD and testing skills. 📚&lt;/p&gt;




&lt;h3&gt;
  
  
  How to Shine in Interviews 🌟
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Show Real-World Examples&lt;/strong&gt;: Share projects where you’ve applied TDD, explaining how it improved the development process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Demonstrate Refactoring&lt;/strong&gt;: Highlight the importance of refactoring and how you’ve used it to enhance the performance and structure of your code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prepare for TDD Coding Tests&lt;/strong&gt;: Practice writing tests first and developing features based on them during technical assessments.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Conclusion 🔑
&lt;/h3&gt;

&lt;p&gt;Test-Driven Development is a crucial skill in today’s development landscape. It ensures &lt;strong&gt;high-quality&lt;/strong&gt;, &lt;strong&gt;maintainable&lt;/strong&gt; software, and companies increasingly prioritize candidates who excel in TDD. Mastering this practice not only helps you write better code but also makes you a standout candidate in the job market.&lt;/p&gt;

</description>
      <category>testing</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Ruby Interview Questions - Part 2</title>
      <dc:creator>Anand Soni</dc:creator>
      <pubDate>Sat, 31 Aug 2024 12:20:52 +0000</pubDate>
      <link>https://dev.to/sonianand11/ruby-interview-questions-part-2-2ace</link>
      <guid>https://dev.to/sonianand11/ruby-interview-questions-part-2-2ace</guid>
      <description>&lt;h3&gt;
  
  
  1. &lt;strong&gt;How would you implement a thread-safe Singleton in Ruby?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. In Ruby, the &lt;code&gt;Singleton&lt;/code&gt; module can be used, but it may not be thread-safe. Here’s a custom implementation using &lt;code&gt;mutex&lt;/code&gt; for thread safety:
&lt;/li&gt;
&lt;/ul&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;MySingleton&lt;/span&gt;
     &lt;span class="vi"&gt;@instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;
     &lt;span class="vi"&gt;@mutex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Mutex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&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;instance&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="vi"&gt;@instance&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@instance&lt;/span&gt;
       &lt;span class="vi"&gt;@mutex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;synchronize&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
         &lt;span class="vi"&gt;@instance&lt;/span&gt; &lt;span class="o"&gt;||=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;
       &lt;span class="k"&gt;end&lt;/span&gt;
       &lt;span class="vi"&gt;@instance&lt;/span&gt;
     &lt;span class="k"&gt;end&lt;/span&gt;

     &lt;span class="nb"&gt;private_class_method&lt;/span&gt; &lt;span class="ss"&gt;:new&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. &lt;strong&gt;Explain how Ruby handles method dispatch and how you can modify it.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ruby handles method dispatch through the method lookup path: first checking the object's singleton class, then its class, then included modules, and finally parent classes.&lt;/li&gt;
&lt;li&gt;You can modify method dispatch using techniques like &lt;code&gt;alias_method&lt;/code&gt;, &lt;code&gt;prepend&lt;/code&gt;, &lt;code&gt;method_missing&lt;/code&gt;, or even overriding &lt;code&gt;respond_to?&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Example using &lt;code&gt;prepend&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt; &lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;LoggerModule&lt;/span&gt;
   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;
     &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Logging: Calling greet"&lt;/span&gt;
     &lt;span class="k"&gt;super&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;MyClass&lt;/span&gt;
   &lt;span class="n"&gt;prepend&lt;/span&gt; &lt;span class="no"&gt;LoggerModule&lt;/span&gt;

   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&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="nb"&gt;puts&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;new&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; Logs the message and then returns "Hello!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;How does garbage collection work in Ruby, and how can you optimize it?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ruby uses a mark-and-sweep garbage collector, which tracks objects in memory and cleans up those no longer in use.&lt;/li&gt;
&lt;li&gt;The generational garbage collection (introduced in Ruby 2.1) improves performance by categorizing objects into generations and focusing on cleaning up younger objects more frequently.&lt;/li&gt;
&lt;li&gt;Optimization techniques include avoiding the creation of unnecessary objects, using symbols instead of strings when appropriate, and utilizing tools like &lt;code&gt;GC.start&lt;/code&gt; judiciously to control when garbage collection occurs.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt; &lt;span class="c1"&gt;# Force garbage collection (usually not recommended in production)&lt;/span&gt;
 &lt;span class="no"&gt;GC&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;

 &lt;span class="c1"&gt;# Tune the garbage collector (for advanced scenarios)&lt;/span&gt;
 &lt;span class="no"&gt;GC&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Profiler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. &lt;strong&gt;What is the difference between &lt;code&gt;Marshal&lt;/code&gt; and &lt;code&gt;YAML&lt;/code&gt; for object serialization in Ruby?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Marshal&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Binary serialization format.&lt;/li&gt;
&lt;li&gt;Faster and more compact than YAML.&lt;/li&gt;
&lt;li&gt;Limited portability (Ruby-specific, not human-readable).&lt;/li&gt;
&lt;li&gt;Example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="highlight ruby"&gt;&lt;code&gt;   &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Ruby"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;version: &lt;/span&gt;&lt;span class="mf"&gt;3.0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="n"&gt;marshaled_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="n"&gt;restored_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;marshaled_data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;YAML&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Text-based serialization format.&lt;/li&gt;
&lt;li&gt;Slower and more verbose, but human-readable and language-agnostic.&lt;/li&gt;
&lt;li&gt;More suitable for configuration files and data exchange between different programming languages.&lt;/li&gt;
&lt;li&gt;Example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="highlight ruby"&gt;&lt;code&gt;   &lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'yaml'&lt;/span&gt;
   &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Ruby"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;version: &lt;/span&gt;&lt;span class="mf"&gt;3.0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="n"&gt;yaml_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_yaml&lt;/span&gt;
   &lt;span class="n"&gt;restored_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;YAML&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yaml_data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. &lt;strong&gt;How would you implement method chaining in Ruby?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Method chaining is a technique where multiple methods are called on an object sequentially in a single statement. Each method returns the object itself (or a modified version of it), allowing the next method to be called.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Calculator&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="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="vi"&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="k"&gt;end&lt;/span&gt;

   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="vi"&gt;@value&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
     &lt;span class="nb"&gt;self&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;subtract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="vi"&gt;@value&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
     &lt;span class="nb"&gt;self&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;result&lt;/span&gt;
     &lt;span class="vi"&gt;@value&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;calc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Calculator&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="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;subtract&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="nf"&gt;result&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 12&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6. &lt;strong&gt;How would you handle large numbers and precision in Ruby?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ruby’s &lt;code&gt;Integer&lt;/code&gt; type automatically handles large numbers by switching from &lt;code&gt;Fixnum&lt;/code&gt; to &lt;code&gt;Bignum&lt;/code&gt;. However, for floating-point numbers, &lt;code&gt;BigDecimal&lt;/code&gt; is preferred to maintain precision in financial calculations or when dealing with very large/small numbers.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt; &lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'bigdecimal'&lt;/span&gt;
 &lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'bigdecimal/util'&lt;/span&gt;

 &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;BigDecimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"1.234567890123456789"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;BigDecimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"0.000000000000000001"&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;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_s&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'F'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; "1.234567890123456790"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  7. &lt;strong&gt;Explain the difference between &lt;code&gt;public&lt;/code&gt;, &lt;code&gt;private&lt;/code&gt;, and &lt;code&gt;protected&lt;/code&gt; methods in Ruby.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Public&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Accessible from anywhere. Methods are public by default unless specified otherwise.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Private&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Can only be called within the context of the current object. Cannot be called with an explicit receiver, even &lt;code&gt;self&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Protected&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Can be called by any instance of the defining class or its subclasses. Unlike private methods, protected methods can be called with an explicit receiver if the receiver is of the same class or subclass.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

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

&lt;pre class="highlight ruby"&gt;&lt;code&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt;
   &lt;span class="kp"&gt;public&lt;/span&gt;

   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;public_method&lt;/span&gt;
     &lt;span class="s2"&gt;"Public"&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;

   &lt;span class="kp"&gt;protected&lt;/span&gt;

   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;protected_method&lt;/span&gt;
     &lt;span class="s2"&gt;"Protected"&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;

   &lt;span class="kp"&gt;private&lt;/span&gt;

   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;private_method&lt;/span&gt;
     &lt;span class="s2"&gt;"Private"&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&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;new&lt;/span&gt;
 &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;public_method&lt;/span&gt;      &lt;span class="c1"&gt;# =&amp;gt; "Public"&lt;/span&gt;
 &lt;span class="c1"&gt;# obj.protected_method  # =&amp;gt; NoMethodError&lt;/span&gt;
 &lt;span class="c1"&gt;# obj.private_method    # =&amp;gt; NoMethodError&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  8. &lt;strong&gt;How would you implement a custom enumerable in Ruby?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To create a custom enumerable, a class must include the &lt;code&gt;Enumerable&lt;/code&gt; module and define an &lt;code&gt;each&lt;/code&gt; method that yields items to a block.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyCollection&lt;/span&gt;
   &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Enumerable&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="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="vi"&gt;@items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;items&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;each&lt;/span&gt;
     &lt;span class="vi"&gt;@items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&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="n"&gt;collection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;MyCollection&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
 &lt;span class="n"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; [2, 4, 6, 8]&lt;/span&gt;
 &lt;span class="n"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:even?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;# =&amp;gt; [2, 4]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  9. &lt;strong&gt;What are fibers in Ruby, and how do they differ from threads?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fibers&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Fibers are a form of lightweight concurrency in Ruby, allowing you to pause and resume code execution manually. They provide finer control over the execution flow but are non-preemptive (the programmer must explicitly yield control).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Threads&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Threads are system-level concurrency primitives. Ruby uses green threads or native threads (depending on the Ruby implementation). Threads can run in parallel (in JRuby or Rubinius) or time-slice (in MRI) but require careful handling of synchronization and resource sharing.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Example of a Fiber:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight ruby"&gt;&lt;code&gt; &lt;span class="n"&gt;fiber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Fiber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
   &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello"&lt;/span&gt;
   &lt;span class="no"&gt;Fiber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;yield&lt;/span&gt;
   &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"World"&lt;/span&gt;
 &lt;span class="k"&gt;end&lt;/span&gt;

 &lt;span class="n"&gt;fiber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resume&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; "Hello"&lt;/span&gt;
 &lt;span class="n"&gt;fiber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resume&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; "World"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;I hope that Helps!!&lt;br&gt;
&lt;a href="https://linktr.ee/sonianand11" rel="noopener noreferrer"&gt;Check out more About me&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>career</category>
      <category>interview</category>
      <category>hiring</category>
    </item>
    <item>
      <title>Ruby Interview Questions - Part 1</title>
      <dc:creator>Anand Soni</dc:creator>
      <pubDate>Mon, 26 Aug 2024 13:28:22 +0000</pubDate>
      <link>https://dev.to/sonianand11/ruby-interview-questions-part-1-44dp</link>
      <guid>https://dev.to/sonianand11/ruby-interview-questions-part-1-44dp</guid>
      <description>&lt;p&gt;When preparing for a Ruby test, especially in an interview setting, it's important to cover key concepts and be ready to demonstrate your knowledge through code. Here are some common questions and sample answers to help you prepare:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;What are the main features of Ruby?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ruby is an object-oriented programming language, which means everything is an object.&lt;/li&gt;
&lt;li&gt;It is dynamically typed and uses garbage collection for memory management.&lt;/li&gt;
&lt;li&gt;Ruby supports multiple programming paradigms, including procedural, object-oriented, and functional programming.&lt;/li&gt;
&lt;li&gt;It has a simple and easy-to-read syntax, emphasising convention over configuration.&lt;/li&gt;
&lt;li&gt;Ruby has a rich standard library and a vibrant ecosystem, particularly around web development with frameworks like Ruby on Rails.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Explain the difference between &lt;code&gt;proc&lt;/code&gt; and &lt;code&gt;lambda&lt;/code&gt; in Ruby.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Both &lt;code&gt;proc&lt;/code&gt; and &lt;code&gt;lambda&lt;/code&gt; are used to create blocks of code that can be stored in variables and passed around, but they behave differently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lambda&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Enforces the number of arguments passed to it. If the wrong number of arguments is provided, it raises an error.&lt;/li&gt;
&lt;li&gt;Returns from itself, not from the method that called it.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Proc&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Does not strictly enforce the number of arguments. Missing arguments will be set to &lt;code&gt;nil&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Returns from the method that is called it, which can cause the enclosing method to return early if not handled properly.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;How does Ruby's garbage collection work?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ruby uses a mark-and-sweep garbage collection algorithm. Objects that are no longer referenced or accessible are "marked" for deletion.&lt;/li&gt;
&lt;li&gt;The garbage collector then "sweeps" through and frees up memory occupied by these unreferenced objects.&lt;/li&gt;
&lt;li&gt;Ruby 2.1 introduced a generational garbage collector, which divides objects into different generations based on their lifespan, optimizing the process by focusing more on newly created objects that are more likely to become garbage.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. &lt;strong&gt;What are symbols in Ruby, and when should you use them?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Symbols in Ruby are immutable, reusable constants represented internally by an integer value. They are often used as identifiers, keys in hashes, or for representing method names.&lt;/li&gt;
&lt;li&gt;Unlike strings, symbols are immutable, which makes them memory-efficient for use cases where the same value is used repeatedly, such as in hash keys or constant values.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. &lt;strong&gt;What is a module in Ruby, and how is it different from a class?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A module in Ruby is a collection of methods and constants. It is similar to a class but cannot be instantiated or inherited.&lt;/li&gt;
&lt;li&gt;Modules are often used for namespacing and as a mixin to add functionality to classes via &lt;code&gt;include&lt;/code&gt; or &lt;code&gt;extend&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The primary difference between a module and a class is that a class can be instantiated to create objects, whereas a module cannot. Modules are also used to achieve multiple inheritance by mixing in functionalities to classes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6. &lt;strong&gt;How do you handle exceptions in Ruby?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ruby uses the &lt;code&gt;begin...rescue...ensure&lt;/code&gt; block to handle exceptions.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;begin&lt;/code&gt; starts the block of code where exceptions might occur.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rescue&lt;/code&gt; defines the code to be run if an exception occurs.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ensure&lt;/code&gt; is an optional block that runs regardless of whether an exception was raised, often used for cleanup activities.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt; &lt;span class="k"&gt;begin&lt;/span&gt;
   &lt;span class="c1"&gt;# Code that might raise an exception&lt;/span&gt;
 &lt;span class="k"&gt;rescue&lt;/span&gt; &lt;span class="no"&gt;StandardError&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
   &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"An error occurred: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;message&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
 &lt;span class="k"&gt;ensure&lt;/span&gt;
   &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"This will run whether an exception occurred or not."&lt;/span&gt;
 &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  7. &lt;strong&gt;Explain the concept of mixins in Ruby.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mixins are a way to share code between classes using modules. By including a module in a class, you can "mix in" the module's methods and constants into that class.&lt;/li&gt;
&lt;li&gt;Mixins allow Ruby to support multiple inheritance since a class can include many modules.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt; &lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Walkable&lt;/span&gt;
   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;walk&lt;/span&gt;
     &lt;span class="s2"&gt;"I'm walking!"&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;Walkable&lt;/span&gt;
 &lt;span class="k"&gt;end&lt;/span&gt;

 &lt;span class="nb"&gt;p&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="nb"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;walk&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; "I'm walking!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  8. &lt;strong&gt;What is &lt;code&gt;self&lt;/code&gt; in Ruby?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;self&lt;/code&gt; refers to the current object instance in Ruby. It is a special variable that points to the object that is currently being operated on.&lt;/li&gt;
&lt;li&gt;Inside an instance method, &lt;code&gt;self&lt;/code&gt; refers to the instance of the class.&lt;/li&gt;
&lt;li&gt;Inside a class method, &lt;code&gt;self&lt;/code&gt; refers to the class itself.&lt;/li&gt;
&lt;li&gt;Understanding &lt;code&gt;self&lt;/code&gt; is crucial for defining instance methods, class methods, and working with scope.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  9. &lt;strong&gt;How do you define and use a singleton method in Ruby?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A singleton method is a method that is defined for a single instance of an object, rather than for all instances of the class.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello"&lt;/span&gt;
 &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;str&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shout&lt;/span&gt;
   &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upcase&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"!!!"&lt;/span&gt;
 &lt;span class="k"&gt;end&lt;/span&gt;

 &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shout&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; "HELLO!!!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  10. &lt;strong&gt;How would you optimize the performance of a Ruby application?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use memoization to avoid recomputation.&lt;/li&gt;
&lt;li&gt;Profile the application to identify bottlenecks using tools like &lt;code&gt;ruby-prof&lt;/code&gt; or &lt;code&gt;StackProf&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Optimize database queries by using eager loading (e.g., &lt;code&gt;includes&lt;/code&gt;) to reduce the number of queries.&lt;/li&gt;
&lt;li&gt;Use caching (e.g., fragment caching, page caching) to reduce the load on the server.&lt;/li&gt;
&lt;li&gt;Optimize code algorithms and logic for efficiency, avoid unnecessary computations, and use efficient data structures.&lt;/li&gt;
&lt;li&gt;Offload expensive tasks to background jobs (e.g., using Sidekiq).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope that Helps!!&lt;br&gt;
&lt;a href="https://linktr.ee/sonianand11" rel="noopener noreferrer"&gt;Checkout more About me&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>career</category>
      <category>rails</category>
      <category>programming</category>
    </item>
    <item>
      <title>Understanding the Decimal Datatype in MySQL</title>
      <dc:creator>Anand Soni</dc:creator>
      <pubDate>Wed, 21 Aug 2024 06:41:54 +0000</pubDate>
      <link>https://dev.to/sonianand11/understanding-the-decimal-datatype-in-mysql-292c</link>
      <guid>https://dev.to/sonianand11/understanding-the-decimal-datatype-in-mysql-292c</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%2Fyb6akmcj5noez7l4bai5.jpg" 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%2Fyb6akmcj5noez7l4bai5.jpg" alt="Image description" width="700" height="256"&gt;&lt;/a&gt;The Decimal datatype in MySQL is essential for finance-related projects, where precision in numeric values is crucial. Recently, I encountered an issue while working with this datatype in one of my Ruby on Rails projects. Let me share my experience and what I learned.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Challenge: Out-of-Range Value Error
&lt;/h2&gt;

&lt;p&gt;During my project, I defined a Decimal field in a migration file and faced the following error:&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;Out&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;range&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="s1"&gt;'db_field'&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This error happened because I tried to insert a value that exceeded the defined range. Here’s the snippet from my migration file:&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;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decimal&lt;/span&gt; &lt;span class="ss"&gt;:rate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;precision: &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;scale: &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;default: &lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This definition corresponds to, meaning the rate column can store values between -0.999 and 0.999. Notice the scale of 3, indicating there are always 3 digits after the decimal point.&lt;/p&gt;

&lt;h2&gt;
  
  
  Breaking Down Precision and Scale
&lt;/h2&gt;

&lt;p&gt;In MySQL, the Decimal datatype is defined as Decimal(P,S), where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;P (Precision): Total number of digits.&lt;/li&gt;
&lt;li&gt;S (Scale): Number of digits after the decimal point.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to Determine Precision and Scale
&lt;/h2&gt;

&lt;p&gt;To decide the appropriate precision and scale, use this simple formula:&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;Precision&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="no"&gt;Scale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Maximum&lt;/span&gt; &lt;span class="n"&gt;digits&lt;/span&gt; &lt;span class="n"&gt;before&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
If you need to store values like 12.345:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You want 2 digits before the decimal point and 3 digits after.&lt;/li&gt;
&lt;li&gt;Total digits (Precision) is 5.&lt;/li&gt;
&lt;li&gt;Therefore, the definition will be Decimal(5,3).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Real-World Application
&lt;/h2&gt;

&lt;p&gt;Here’s an example from my project:&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;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decimal&lt;/span&gt; &lt;span class="ss"&gt;:rate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;precision: &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;scale: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;default: &lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, I needed to store values up to 999.99. Hence, Decimal(5,2) allowed me to have 3 digits before the decimal point and 2 digits after.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;p&gt;For more detailed information, check out the &lt;a href="https://dev.mysql.com/doc/refman/8.0/en/fixed-point-types.html" rel="noopener noreferrer"&gt;MySQL Documentation&lt;/a&gt; on Decimal Datatype. It’s a great resource for understanding how to work with Decimal and other numeric types.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understand&lt;br&gt;
ing the Decimal datatype in MySQL is vital for accurately handling numeric data in your applications. By setting the correct precision and scale, you can avoid common errors and ensure data integrity. I hope this explanation helps you in your projects!&lt;/p&gt;

</description>
      <category>mysql</category>
      <category>database</category>
      <category>mariadb</category>
      <category>aws</category>
    </item>
    <item>
      <title>Solving Cypress Preflight Request Errors in Rails Test Environment</title>
      <dc:creator>Anand Soni</dc:creator>
      <pubDate>Wed, 21 Aug 2024 06:22:39 +0000</pubDate>
      <link>https://dev.to/sonianand11/solving-cypress-preflight-request-errors-in-rails-test-environment-3l5j</link>
      <guid>https://dev.to/sonianand11/solving-cypress-preflight-request-errors-in-rails-test-environment-3l5j</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;While developing and testing web applications, encountering errors is a common part of the process. Recently, I stumbled upon an issue while writing test scripts in Cypress and running a Rails server in the Test environment. The error messages were perplexing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"No content available for preflight request in Chrome."&lt;/li&gt;
&lt;li&gt;"ERR_EMPTY_RESPONSE."&lt;/li&gt;
&lt;li&gt;"Failed to load response data. No data found with the resource with a given identifier."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Despite checking my CORS configuration, everything seemed to be in order. However, the error persisted, hindering communication between the browser and the server. Oddly enough, this issue only arose when the server was running in the Test environment, while everything worked smoothly in the Development environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem: A Strange Behavior in the Test Environment
&lt;/h2&gt;

&lt;p&gt;The error was quite frustrating, as it only occurred in the Test environment. This discrepancy led me to investigate the root cause further. After scouring various forums and documentation, I discovered that the issue was tied to how the browser was communicating with the server - specifically, how the server was being accessed.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Culprit: Localhost vs. 127.0.0.1
&lt;/h2&gt;

&lt;p&gt;The problem boiled down to the usage of the term "localhost." In the Development environment, the Rails server accessed via localhost worked perfectly fine with Cypress. However, the Test environment localhost was causing these preflight errors.&lt;/p&gt;

&lt;p&gt;After some trial and error, I found a simple yet effective solution: replacing localhost it with 127.0.0.1 in the API URL. This change resolved the issue immediately, allowing Cypress to communicate with the Rails server without any errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Did This Work?
&lt;/h2&gt;

&lt;p&gt;The difference between localhost and 127.0.0.1 is subtle but significant. Although both point to the local machine, localhost is resolved via the operating system's host file, which 127.0.0.1 is a direct IP address reference. In some cases, especially in testing environments, the resolution of localhost might introduce issues that aren't present when using the raw IP address.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;If you encounter similar preflight request errors while using Cypress with a Rails server in the Test environment, consider swapping localhost with 127.0.0.1 in your API URLs. This small change can save you hours of debugging and ensure that your testing process runs smoothly.&lt;/p&gt;

&lt;p&gt;By sharing this experience, I hope to help others avoid the same frustration and get back to what we love doing - building great software.&lt;/p&gt;

&lt;p&gt;LinkedIn connect: &lt;a href="https://www.linkedin.com/in/anandsoni11/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/anandsoni11/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cypress</category>
      <category>testing</category>
      <category>selenium</category>
      <category>playwright</category>
    </item>
    <item>
      <title>How to Use Mutex in Ruby: A Comprehensive Guide</title>
      <dc:creator>Anand Soni</dc:creator>
      <pubDate>Wed, 21 Aug 2024 05:07:59 +0000</pubDate>
      <link>https://dev.to/sonianand11/how-to-use-mutex-in-ruby-a-comprehensive-guide-18aa</link>
      <guid>https://dev.to/sonianand11/how-to-use-mutex-in-ruby-a-comprehensive-guide-18aa</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%2Fzraaq1d9eqi503ko737a.jpg" 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%2Fzraaq1d9eqi503ko737a.jpg" alt="Image description" width="576" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Concurrency is a powerful tool in programming, enabling multiple threads to execute code simultaneously. However, with this power comes the responsibility to manage shared resources safely. In Ruby, Mutex (short for mutual exclusion) is a key component in ensuring that only one thread can access a resource at a time, preventing potential data corruption or unpredictable behaviour.&lt;br&gt;
In this blog, we'll explore how to use Mutex in Ruby, supported by sample code and a real-life scenario to illustrate its practical application.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is a Mutex?
&lt;/h2&gt;

&lt;p&gt;A Mutex is an object used to manage the synchronization of threads. When one thread locks a Mutex, any other thread that attempts to lock the same Mutex will be put on hold until the first thread releases it. This mechanism ensures that critical sections of code, where shared resources are accessed, are executed by only one thread at a time.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why Use a Mutex?
&lt;/h2&gt;

&lt;p&gt;Imagine a scenario where multiple threads are modifying the same variable or writing to the same file. Without proper synchronization, the result could be unpredictable or incorrect. A Mutex helps to avoid such issues by ensuring that only one thread can access the shared resource at any given time.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to Use Mutex in Ruby
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'thread'&lt;/span&gt;

&lt;span class="c1"&gt;# Initialize a Mutex&lt;/span&gt;
&lt;span class="n"&gt;mutex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Mutex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;

&lt;span class="c1"&gt;# Shared resource&lt;/span&gt;
&lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="c1"&gt;# Create threads&lt;/span&gt;
&lt;span class="n"&gt;threads&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;times&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;times&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="c1"&gt;# Lock the mutex before modifying the shared resource&lt;/span&gt;
      &lt;span class="n"&gt;mutex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;synchronize&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
        &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&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="c1"&gt;# Wait for all threads to finish&lt;/span&gt;
&lt;span class="n"&gt;threads&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:join&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Final counter value: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;counter&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;
  
  
  In this example:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We initialize a Mutex object.&lt;/li&gt;
&lt;li&gt;We create a shared resource (counter) that will be accessed by multiple threads.&lt;/li&gt;
&lt;li&gt;We create 10 threads, each incrementing the counter 1000 times.&lt;/li&gt;
&lt;li&gt;Inside the mutex.synchronize block, we ensure that only one thread can modify the counter at a time.&lt;/li&gt;
&lt;li&gt;Finally, we print the final value of counter, which should be 10000 if the Mutex has properly synchronized the access.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Real-Life Scenario: Managing Bank Account Transactions
&lt;/h2&gt;

&lt;p&gt;To understand the real-life application of Mutex, let's consider a scenario where multiple threads represent transactions on a bank account. Each transaction may involve depositing or withdrawing money, and we must ensure that the account balance remains accurate.&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;require&lt;/span&gt; &lt;span class="s1"&gt;'thread'&lt;/span&gt;

&lt;span class="c1"&gt;# Initialize a Mutex&lt;/span&gt;
&lt;span class="n"&gt;account_mutex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Mutex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;

&lt;span class="c1"&gt;# Bank account class&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BankAccount&lt;/span&gt;
  &lt;span class="nb"&gt;attr_reader&lt;/span&gt; &lt;span class="ss"&gt;:balance&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="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&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;deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@balance&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;amount&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;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@balance&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;amount&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;# Shared bank account&lt;/span&gt;
&lt;span class="n"&gt;account&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;BankAccount&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="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Transactions&lt;/span&gt;
&lt;span class="n"&gt;threads&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="c1"&gt;# Deposit thread&lt;/span&gt;
&lt;span class="n"&gt;threads&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;times&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;account_mutex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;synchronize&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&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;# Withdraw thread&lt;/span&gt;
&lt;span class="n"&gt;threads&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;times&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;account_mutex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;synchronize&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&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;# Wait for all threads to finish&lt;/span&gt;
&lt;span class="n"&gt;threads&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:join&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Final account balance: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;balance&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;
  
  
  In this scenario:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We define a BankAccount class with deposit and withdraw methods.&lt;/li&gt;
&lt;li&gt;We create a shared BankAccount instance with an initial balance of 1000.&lt;/li&gt;
&lt;li&gt;We create two threads: one for depositing money and one for withdrawing money.&lt;/li&gt;
&lt;li&gt;We use a Mutex to synchronize access to the deposit and withdraw methods, ensuring that only one transaction can modify the account balance at a time.&lt;/li&gt;
&lt;li&gt;Finally, we print the final account balance, which should accurately reflect all transactions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Using Mutex in Ruby is essential when dealing with concurrency and shared resources. It provides a simple yet effective way to ensure that only one thread can access a critical section of code at a time, preventing potential issues like data corruption or race conditions.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>SOLID principles using some fun analogies with Vehicle Example</title>
      <dc:creator>Anand Soni</dc:creator>
      <pubDate>Wed, 21 Aug 2024 04:52:17 +0000</pubDate>
      <link>https://dev.to/sonianand11/solid-principles-using-some-fun-analogies-with-vehicle-example-34p7</link>
      <guid>https://dev.to/sonianand11/solid-principles-using-some-fun-analogies-with-vehicle-example-34p7</guid>
      <description>&lt;h2&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%2Ft6m9qsbaerrwfsavhjer.jpg" alt="Image description" width="625" height="417"&gt;
&lt;/h2&gt;

&lt;p&gt;SOLID is an acronym for a group of five good principles (rules) in computer programming. SOLID allows programmers to write code that is easier to understand and change later on. SOLID is often used with systems that use an &lt;a href="https://simple.wikipedia.org/wiki/Object-oriented_design" rel="noopener noreferrer"&gt;object-oriented design&lt;/a&gt;.&lt;br&gt;
Let's explain the SOLID principles using the vehicle example. Imagine we're designing a system to manage different types of vehicles, like cars and electric cars, for a transportation service.&lt;/p&gt;
&lt;h2&gt;
  
  
  S - Single Responsibility Principle (SRP)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Vehicle Example&lt;/strong&gt;: Imagine you have a car. It's responsible for driving, but it shouldn't be responsible for handling its own maintenance (like oil changes or tyre rotations). Instead, a separate mechanic is responsible for that.&lt;br&gt;
&lt;strong&gt;Explanation&lt;/strong&gt;: In our code, the Vehicle class should only handle things related to the vehicle itself, like storing its make and model. If we need to manage maintenance, we create a separate Maintenance class for that. This way, each class has one job or responsibility, making the code easier to manage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Vehicle
  def initialize(make, model)
    @make = make
    @model = model
  end
end

class Maintenance
  def initialize(vehicle)
    @vehicle = vehicle
  end

  def perform_maintenance
    puts "Performing maintenance on #{@vehicle.make} #{@vehicle.model}"
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  O - Open/Closed Principle (OCP)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Vehicle Example&lt;/strong&gt;: Suppose you have a basic car, and now you want to add an electric car to your system. You shouldn't have to modify the existing car class to add features for electric cars. Instead, you can extend the existing functionality by creating a new class for electric cars.&lt;br&gt;
&lt;strong&gt;Explanation&lt;/strong&gt;: The Vehicle class is open for extension (you can create new types of vehicles like ElectricVehicle), but it's closed for modification (you don't need to change the Vehicle class itself to add new types).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Vehicle
  def initialize(make, model)
    @make = make
    @model = model
  end

  def description
    "#{@make} #{@model}"
  end
end

class ElectricVehicle &amp;lt; Vehicle
  def initialize(make, model, battery_range)
    super(make, model)
    @battery_range = battery_range
  end

  def description
    "#{super} with #{@battery_range} miles battery range"
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  L - Liskov Substitution Principle (LSP)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Vehicle Example&lt;/strong&gt;: Imagine you have a fleet of vehicles, and you can replace any regular car with an electric car without any issues. Both should be able to perform their basic function - driving - without breaking the system.&lt;br&gt;
&lt;strong&gt;Explanation&lt;/strong&gt;: Any subclass (like ElectricVehicle) should be able to replace its parent class (Vehicle) without altering the behaviour of the program. This ensures that our code can handle different types of vehicles in the same way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Vehicle
  def initialize(make, model)
    @make = make
    @model = model
  end

  def drive
    puts "Driving the #{@make} #{@model}"
  end
end

class ElectricVehicle &amp;lt; Vehicle
  def drive
    puts "Driving the electric #{@make} #{@model} quietly"
  end
end

def test_drive(vehicle)
  vehicle.drive
end

car = Vehicle.new("Toyota", "Corolla")
ev = ElectricVehicle.new("Tesla", "Model 3")

test_drive(car)  # Driving the Toyota Corolla
test_drive(ev)   # Driving the electric Tesla Model 3 quietly
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  I - Interface Segregation Principle (ISP)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Vehicle Example&lt;/strong&gt;: Imagine you have different types of vehicles: some can be charged (like electric cars), and some can only be driven (like gas cars). You don't want a gas car to have to deal with charging-related methods.&lt;br&gt;
&lt;strong&gt;Explanation&lt;/strong&gt;: Classes should only implement the interfaces (or behaviours) they need. For example, an ElectricVehicle might implement both Drivable and Chargeable interfaces, while a regular Vehicle only implements Drivable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module Drivable
  def drive
    raise NotImplementedError, "This #{self.class} cannot drive"
  end
end

module Chargeable
  def charge
    raise NotImplementedError, "This #{self.class} cannot be charged"
  end
end

class Vehicle
  include Drivable

  def initialize(make, model)
    @make = make
    @model = model
  end

  def drive
    puts "Driving the #{@make} #{@model}"
  end
end

class ElectricVehicle &amp;lt; Vehicle
  include Chargeable

  def initialize(make, model, battery_range)
    super(make, model)
    @battery_range = battery_range
  end

  def drive
    puts "Driving the electric #{@make} #{@model} quietly"
  end

  def charge
    puts "Charging the #{@make} #{@model}"
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  D - Dependency Inversion Principle (DIP)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Vehicle Example&lt;/strong&gt;: Imagine a car can have different types of engines: a gas engine or an electric engine. Instead of directly depending on a specific engine type, the car should depend on a more general Engine interface so it can use any type of engine.&lt;br&gt;
&lt;strong&gt;Explanation&lt;/strong&gt;: High-level modules (like the Vehicle) should not depend on low-level modules (like GasEngine or ElectricEngine). Both should depend on abstractions (like an Engine interface). This makes the system more flexible and easier to change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Engine
  def start
    raise NotImplementedError, "This #{self.class} cannot start"
  end
end

class GasEngine &amp;lt; Engine
  def start
    puts "Starting gas engine"
  end
end

class ElectricEngine &amp;lt; Engine
  def start
    puts "Starting electric engine"
  end
end

class Vehicle
  def initialize(engine)
    @engine = engine
  end

  def start
    @engine.start
  end
end

gas_engine = GasEngine.new
electric_engine = ElectricEngine.new

gas_car = Vehicle.new(gas_engine)
electric_car = Vehicle.new(electric_engine)

gas_car.start        # Starting gas engine
electric_car.start   # Starting electric engine

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

&lt;/div&gt;



&lt;p&gt;By following the SOLID principles in this vehicle example, we can build a system that is easy to maintain, extend, and adapt to new requirements.&lt;/p&gt;

&lt;p&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/anandsoni11/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/anandsoni11/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>solidprinciples</category>
      <category>agile</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
