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

          Create dynamic sitemap on ruby on rails

          Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site. It’s basically a XML file describing all URLs in your page: The following example shows a Sitemap that contains just one URL and uses all optional tags. The optional tags are in italics. <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">    <url>       <loc>http://www.example.com/</loc>       <lastmod>2005-01-01</lastmod>       <changefreq>monthly</changefreq>     ...

          Omniauth Linked in Ruby On Rails

          def get_linkedin_user_data      omniauth = request.env["omniauth.auth"]      dat=omniauth.extra.raw_info      linked_app_key = "xxxxxxx"      linkedin_secret_key = "yyyyyyy"      client = LinkedIn::Client.new(linked_app_key,linkedin_secret_key)      client.authorize_from_access(omniauth['credentials']['token'],omniauth['credentials']['secret'])      connections=client.connections(:fields => ["id", "first-name", "last-name","picture-url"])      uid=omniauth['uid']      token=omniauth["credentials"]["token"]      secret=omniauth["credentials"]["secret"]   #linked user data     omniauth = request.env["omniauth.auth"]      data             = omniauth.info      user_name...

          Install Rvm on ubuntu

          sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev curl -L https://get.rvm.io | bash -s stable source ~/.rvm/scripts/rvm rvm install 2.0.0-p645 rvm use 2.0.0-p645 --default ruby -v rvm gemset create rails3.2.8 rvm gemset use rails3.2.8 gem install rails -v 3.2.8