Posts

Caching With Rails

Image
 Caching With Rails Caching in Rails is a performance optimization technique that stores copies of objects. It improves the application's performance by reducing response times and server loads. Rails uses several types of caching and it integrates well with caching stores like Memcached , Redis , or even the File System . Let's understand some of the caching types in detail: Fragment Caching : Fragment caching allows to caching of the specific parts of views, rather than the entire view. This is useful when you have dynamic sections, such as a post that has comments.     <% @comments.each do |comment| %>       <% cache comment do %>      <% end %> When the request is received for the first time, the system stores a cached HTML copy of the comments, using the updated_at attribute as part of the filename. On subsequent requests, it checks if the updated_at value for the comment has changed. If it hasn’t, the cached version is...

Arrays in Ruby | Exploring Essential Array Methods in Ruby

Image
  Array In Ruby arrays, ordered collection of objects. They can hold any combination of objects including integers, strings, symbols, and even other arrays. Here's how arrays work in Ruby:  Creating an array Array can be created using the ` []` or ` Array.new`  method. using square braces           arr1 = [1, 2, 3]  using Array.new           arr2 = Array.new(3) # Creates an array with 3 nil elements                   Access the array of elements In Ruby, array elements can be accessed by their index, starting from 0. We can use the ` [] ` or the ` slice` method. using square braces           arr1 = [1, 2, 3]           p arr1[0] # output: 1           p arr1[0..2]  # output:  [1, 2, 3]              using the ` slice...

Metaprogramming in Ruby | How metaprogramming works in Ruby? | Techniques in Metaprogrammming?

Image
  Metaprogramming is a powerful technique that allows me to write or modify the existing code during runtime. Ruby provides powerful features for metaprogramming, making it possible to dynamically define classes, methods, and perform other tasks. Here are some common techniques that are used in metaprogramming in Ruby: Dynamic method definition:  Ruby allows methods to be defined dynamically, using define_method. While  describing the class, we defined its structure and behavior. This includes defining instance methods. By using the ` define_method` we instruct Ruby to create a method with the given name and implementation dynamically at runtime.     class Student          define_method :info do              puts "My name is Yash" #dynamically defined method          end      end      student = Student.new      student.info In t...

Turbo Streams In Rails | How to use turbo streams in rails?

Image
  In Rails 7, hotwire is the default front-end framework, this means we don't need to install additional gems to the application. It is used to send HTML over the wire. It is a collection of tools that simplifies building web applications in Rails, without relying heavily on Javascript. It sends HTML over the wire instead of JSON like traditional single-page applications (SPAs). TURBO STREAMS: It is used to add real-time update features to the applications. It helps to add real-time features using CRUD actions. Ok so let's start using the turbo streams in our application. Create a new application in rails rails new railshotwire Create a Product model: rails g model Product name:string rails db:migrate Create a ` products ` controller rails g controller Products Within the products controller, define the create and index actions as follows:      # app/controllers/products_controller.rb      class ProductsController < ApplicationController   ...

Object References In Ruby | Clone and Dup in Ruby

Image
 Object References In Ruby In Ruby,  clone  and  dup  are used to create shallow copies of an object, but they behave slightly differently. Both are used to create a copy of an object, but they have somewhat different behavior. 1. Dup: The dup method will create a shallow copy of an object. A shallow copy means the object itself is duplicated, any object references by the original object are not duplicated. Changes made to the cloned object will not affect the original object. Dup allows modifications to the frozen state object. statement1 = "What's up".freeze statement2 = statement1.dup statement2 << " Nice to meet you" puts statement1 # Output: What's up puts statement2 # Output: What's up Nice to meet you # FROZEN object statement1 = "What's up".freeze statement2 = statement1.dup statement2 << " Nice to meet you" puts statement1 # Output: What's up puts statement2 # Output: What's up Nice to meet you 2. Clon...

Include, Extend and Prepend in Ruby | Mixins in Ruby

Image
  In Ruby, include , extend, and prepend keywords are used to mix modules in class. In Ruby multiple inheritance is not supported, modules are mixed with classes to achieve this functionality. This is also known as Mixins. This approach not only provides code reuse, and modularity but also promotes a more flexible codebase. The use of mixins enhances code readability and maintainability. In Ruby, include, extend, and prepend are three different ways to mix functionality from modules into classes. Each has its own distinct purpose and behavior: 1. Include Include keyword is used to bring all methods inside the class. This means all the methods are included as instance methods. They can be called with an object of that class.  module A     def greet         puts "Greet from instance method"     end end class B     include A end a = B.new a.greet 2. Extend The extend keyword is used to bring all the methods inside the clas...

Types of arguments in Ruby | Parameters in Ruby

Image
  In Ruby, arguments can be classified into several types based on how they are passed to the methods. Let's see each argument type with an example: 1. Required arguments: This argument must be provided when calling a method, otherwise, it will throw the  argument error.      def greet(name, country)          puts "My name is  #{name} and I am from #{country}"      end      greet("Yash", "India") If we pass only one argument it will throw the below error           `greet': wrong number of arguments (given 1, expected 2) (ArgumentError) 2. Default arguments: Default arguments have predefined values that will be used when no arguments are passed to the method.      def greet(name="Yash")          puts "My name is  #{name} "      end      greet 3. Variable arguments: They allow a method to accept a...