Wednesday, 10 April 2019

Ruby On Rails Questions & Answers(List 2)

1. Single table inheritance
In Single-Table Inheritance (STI), many subclasses inherit from one superclass with all the data in the same table in the database. The superclass has a “type” column to determine which subclass an object belongs to.
    class Vehicle < ApplicationRecord
    end
    class Bicycle < Vehicle
    end
    class Motorcycle < Vehicle
    end
    class Car < Vehicle
    end

In a polymorphic association, one model “belongs to” several other models using a single association. Each model, including the polymorphic model, has its own table in the database.
    class Post
     belongs_to :postable, polymorphic: true
    end
    class Person
     has_many :posts, as :postable
    end
    class Group
     has_many :posts, as :postable
    end


2. Design Patterns In Rails
Basic design patterns in Ruby on Rails. software design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can > be transformed directly into source or machine code.
    1. Services
    2. Decorator
    3. Serializer

3. Singleton methods in ruby
A singleton method is a method that is defined only for a single object.
Instance methods are methods of a class (i.e. defined in the class's definition). Class methods are singleton methods on the Class instance of a class -- they are not defined in the class's definition. Instead, they are defined on the singleton class of the object.

    foo = Array.new
    def foo.size
      "Hello World!"
    end
    foo.size  # => "Hello World!"
    foo.class # => Array


    #Create another instance of Array Class and call size method on it
    bar = Array.new
    bar.size  # => 0
    foo.singleton_methods  # => [:size]
    bar.singleton_methods  # => []


4.class_eval v/s instance_eval
    Foo = Class.new
    Foo.class_eval do
      def class_bar
        "class_bar"
      end
    end
    Foo.instance_eval do
      def instance_bar
        "instance_bar"
      end
    end
    Foo.class_bar       #=> undefined method ‘class_bar’ for Foo:Class
    Foo.new.class_bar   #=> "class_bar"
    Foo.instance_bar       #=> "instance_bar"
    Foo.new.instance_bar   #=> undefined method ‘instance_bar’ for #<Foo:0x7dce8>


    class_eval
    mod.class_eval(string [, filename [, lineno]]) => obj
    Evaluates the string or block in the context of mod. This can be used to add methods to a class.

    instance_eval
    obj.instance_eval {| | block } => obj


    Evaluates a string containing Ruby source code, or the given block, within the context of the receiver (obj). In order to set the context, the variable self is set to obj while the code is executing, giving the code access to obj’s instance variables.

5. Include vs Extend in Ruby
Now that we know the difference between an instance method and a class method, let’s cover the difference between include and extend in regards to modules. Include is for adding methods to an instance of a class and extend is for adding class methods. Let’s take a look at a small example.
    module Foo
      def foo
        puts 'heyyyyoooo!'
      end
    end

    class Bar
      include Foo
    end

    Bar.new.foo # heyyyyoooo!
    Bar.foo # NoMethodError: undefined method ‘foo’ for Bar:Class

    class Baz
      extend Foo
    end

    Baz.foo # heyyyyoooo!
    Baz.new.foo # NoMethodError: undefined method ‘foo’ for #<Baz:0x1e708>

No comments:

Post a Comment

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...