Skip to main content

Ruby on Rails Interview Questions Experienced

ROR Interview Questions Experienced 

  1. Difference between the map & each?
    • The map will return the new array with modified values.
    • Each will return the same array with an object.
  2. Write the  program to sort the array in ruby without using the sort method?
    • def ary_sort(list, new_array = nil)
      return new_array if list.size <= 0
      if new_array == nil
      min = list.min
      new_array = [] end
      ary_sort(list, new_array)
      new_array << min list.delete(min)
      end  
    • ary_sort([1,6,5,3,8,24,18])
  3. Write the   Program for array pass the arguments? 
    1. Input will be a = [2,3,4] ,a.add_no(2)   ====> Output - [4,5,6]
    • Array.class_eval do
      def add_no(n)
      self.map{|x| x+ n}
      end
      end
  4. Difference between the find and find_by in rails?
    • find_by() returns the nil if the record does not exist in the database
    • find() returns the  ActiveRecord::RecordNotFound exception f the record does not exist in the database.
  5. How do access the private method outside the class?
    • class Koti
    •  private 
    •   def test_priv
    •     puts "this is the private method"
    •   end
    • end
    • Koti.new.instance_eval(:test_priv)
  6. Given two arrays of integers nums and index. Your task is to create a target array under the following rules:
    1. * Initially target array is empty.
    2. * From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array.
    3. * Repeat the previous step until there are no elements to read in nums and index.
    4. Return the target array.
    5. Example 1:
    6. Input: nums = [0,1,2,3,4], index = [0,1,2,2,1]
    7. Output: [0,4,1,3,2]

    • Explanation:
    • nums       index     target
    • 0            0        [0]
    • 1            1        [0,1]
    • 2            2        [0,1,2]
    • 3            2        [0,1,3,2]
    • 4            1        [0,4,1,3,2]


    • Example 2:
    • Input: nums = [1,2,3,4,0], index = [0,1,2,3,0]
    • Output: [0,1,2,3,4]
    • Explanation:
    • nums       index     target
    • 1            0        [1]
    • 2            1        [1,2]
    • 3            2        [1,2,3]
    • 4            3        [1,2,3,4]
    • 0            0        [0,1,2,3,4]
  7. What is the garbage collection?
    • Garbage collection allows the removal of the pointer values that is left behind when the execution of the program ends. -It frees the programmer from tracking the object that is being created dynamically on runtime
    • The Ruby garbage collector also allocates memory. It’s a complete memory management system.
  8. What is the difference between the attr_accessor,attr_accesible?
    1. attr_accessor -> attr_accessor is a Ruby method that makes a getter and a setter.
    2. attr_accesible -> attr_accesible is Rails method that allows you to pass in values to a mass assignment: new(attrs) or update_attributes(attrs)
    3. attr_accessor is ruby code and is used when you do not have a column in your database, but still want to show a field in your forms. The only way to allow this is to attr_accessor :fieldname
    4. attr_accessor is gettersetter method. whereas attr_accessible is to say that particular attribute is accessible or not. we should use the Strong parameter instead of attr_accessible to protect from mass assignment.
    5. attr_accessor example
      • class Address
            attr_accessor :city  
            def initialize
                @city = ""
            end
        end
      •  Address.new.city  = "Miryalaguda"
      • class Person
          def name
            @name
          end
        
          def name=(str)
            @name = str
          end
        end
        
        person = Person.new
        person.name = 'kotesh'
        person.name # => "kotesh"
      • class Person
          attr_accessor :name
        end
      • person = Person.new
        person.name = "koti"
        person.name # => "koti"
  9. Write the program for the below input
    1. Write the program for the below input
      • Input data is ===> array = [1, 2] snap = Snapshot.new(array) array[0] = 3; array = snap.restore() puts array print(array.join()) #It should log "1,2" array.push(4) array = snap.restore() print(array.join()) #It should log "1,2"
      • Program is
      • class Snapshot def initialize(inp) @start_val =[] inp.each{|x| @start_val << x} @start_val.freeze end def restore() new_strt_val = [] @start_val.each{|y| new_strt_val<< y } return new_strt_val end end 
  10. What are the design patterns in ruby?
    • Service Objects
      View Objects
      Query Objects
      Decorators
       Form Objects
      Value Objects
      Policy Objects 
  11. Write the program for palindrome in ruby?
      • a = "RKR"
        new_ar = []
        a.each_with_index do |i,index|
         new_ar << a[(a.length-1) - index]
        end
        if a == new_ar.join(",")
          puts "palindrome number"
        else
          puts "It's not palindrome number"
        end
  12. How to pass the block in the method using ruby?
    • def method1 puts yield end method1{"testing block with arguments"}
  13. Difference between the proc & block?
    • Blocks are used extensively in Ruby for passing bits of code to functions
    •  Procs behave like blocks, but they can be stored in a variable.
    • A block is a collection of code enclosed in a do/end statement or between braces
    • def block_method
      puts "we are in the method"
      end
  14. Given array how many times "1" is repeated?
    • /p => a = [1,0,1,1,1,1,0,1] O/p => 6 Solution: a.collect{|x|x if x == 1}.inject(:+) a.count(1) a.inject(:+).
  15. What is the use of webpackers in rails?
    • Webpacker is a gem that allows easy integration of JavaScript pre-processor and bundler with Rails. It provides various helpers and configuration options to use webpack easily with Rails
    • <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>

          Comments

          Popular posts from this blog

          Error malloc(): memory corruption nginx with passenger?

          Error malloc(): memory corruption nginx with passenger Passenger issue resolving steps :  sudo gem uninstall passenger(uninstall all passenger) sudo gem install passenger sudo passenger-install-nginx-module --auto --auto-download --prefix=/opt/nginx --extra-configure-flags=none Update nginx config file with new passenger version and restart the nginx

          Lazy loading in rails – Rails Feature

           Lazy loading in rails – Rails Feature ? Lazy loading in rails is the amazing feature provided with rails. In console you might have tried to examine how lazy loading in rails actually works. In this tutorial, we will learn about this Rails - Lazy loading feature with examples. What exactly is Lazy Loading? As the name suggests the data is loaded in lazy manner (Really!) i.e. Your database is queried only when data from the database is required for some kind of manipulation in code. You will get more of this after you read how-to of lazy loading below. How lazy loading works: Whenever you try to get some data from database, For example, users is the database table that you have. And you are querying database to get users having age less than 20. Then, you will write code like, result = User.where("age < 20") when above statement is executed, your database is not queries yet(because the resultant data is not required yet). When you execute following code, records = resu...

          Rails Migration Difference between Text and String

          Rails Migration Difference between Text and String ? While working with Rails Migration Difference between Text and String is important to be known to every developer. Columns and their data types are finalized while deciding Table structure. This tutorial will help understand difference between String and Text column type and illustrate how to write Rails Migration implementing the same. You might want to read about database.yml files for specifying database configuration for Rails Application. 1. Concepts When String or Text data type is required?     Whenever you require your column to store information which is lengthy in size (Many characters), you need to consider String or Text data type for the column.     Both of them let you store Many(How Many - will see later) characters Difference between String and Text Considering MySQL database Feature     String     Text Length     1 to 255     ...