chrishein / daxx_rails_interview_questions

Answers for Interview Questions suggested by DAXX (2014)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Answers for Interview Questions suggested by DAXX (2014)

Answers for questions suggested in the article How to Hire the Right Offshore Ruby on Rails Developer: Ruby on Rails Interview Questions.

You may know all the answers and might have used most of these in your day to day work, but it helps to write it down again so that you may put it in words when interviewed.

Note that the article was published July 21, 2014.

Questions

Ruby Interview Questions

  1. How do you define class, instance, and global variables?

    • Class Variables
    class Song
      @@plays = 0
    end

    Important note on this and Class Level Instance Variables here.

    • Instance variables
    class Person
      def initialize
        @name = "Tom"
      end
    end

    Or if you want it defined with getter and setter methods

    class Person
      attr_accessor :name
    
      def initialize
        @name = "Tom"
      end
    end

    More on attr_accessor.

    • Global Variables
    $global_variable = 10
  2. What is rubygems?

    RubyGems.org is the Ruby community’s gem hosting service, where everyone can instantly publish gems and then install them.

  3. What is a module?

    From Ruby Docs.

    A Module is a collection of methods and constants. The methods in a module may be instance methods or module methods. Instance methods appear as methods in a class when the module is included, module methods do not. Conversely, module methods may be called without creating an encapsulating object, while instance methods may not.

    From rubylearning.com: Modules serve two purposes:

    • They act as namespace:
    # p058mytrig.rb  
    module Trig  
      PI = 3.1416  
      # class methods  
      def Trig.sin(x)  
        # ...  
      end  
      def Trig.cos(x)  
        # ...  
      end  
    end  
    
    # p059mymoral.rb  
    module Moral  
      VERY_BAD = 0  
      BAD         = 1  
      def Moral.sin(badness)  
        # ...  
      end  
    end  
    
    # p060usemodule.rb  
    require_relative 'p058mytrig'  
    require_relative 'p059mymoral'  
    Trig.sin(Trig::PI/4)  
    Moral.sin(Moral::VERY_BAD)
    • They allow you to share functionality between classes (mixins):
    module Persister
      def save_to_file
        ...
      end
    end
    
    class WorkSheet
      include Persister
    
      def do_stuff
        ...
      end
    end
    
    worksheet = WorkSheet.new
    worksheet.do_stuff
    worksheet.save_to_file
  4. What is a Range?

    From Ruby Docs.

    A Range represents an interval—a set of values with a beginning and an end. Ranges may be constructed using the s..e and s...e literals, or with ::new. Ranges constructed using .. run from the beginning to the end inclusively. Those created using ... exclude the end value. When used as an iterator, ranges return each value in the sequence.

    (-1..-5).to_a      #=> []
    (-5..-1).to_a      #=> [-5, -4, -3, -2, -1]
    ('a'..'e').to_a    #=> ["a", "b", "c", "d", "e"]
    ('a'...'e').to_a   #=> ["a", "b", "c", "d"]
  5. What is a Symbol?

    From Ruby Docs.

    Symbol objects represent names and some strings inside the Ruby interpreter. They are generated using the :name and :"string" literals syntax, and by the various to_sym methods. The same Symbol object will be created for a given name or string for the duration of a program's execution, regardless of the context or meaning of that name. Thus if Fred is a constant in one context, a method in another, and a class in a third, the Symbol :Fred will be the same object in all three contexts.

    (-1..-5).to_a      #=> []
    (-5..-1).to_a      #=> [-5, -4, -3, -2, -1]
    ('a'..'e').to_a    #=> ["a", "b", "c", "d", "e"]
    ('a'...'e').to_a   #=> ["a", "b", "c", "d"]
  6. How are a Symbol and a String different?

    The same Symbol object will be created for a given name or string for the duration of a program's execution. This is not true with strings.

    :my_symbol.object_id  #=> 544168
    :my_symbol.object_id  #=> 544168
    :my_symbol.object_id  #=> 544168
    
    "my_string".object_id #=> 70230778661160
    "my_string".object_id #=> 70230778572940
    "my_string".object_id #=> 70230778536700
  7. How do “and” and “&&” operators differ?

    From Difference between “and” and && in Ruby?.

    and is the same as && but with lower precedence. They both use short-circuit evaluation.

    WARNING: and even has lower precedence than = so you'll want to avoid and always

  8. How do you define a custom Exception?

    class MyError < StandardError
    end

Rails Interview Questions

  1. What is Scope in Rails?

    As stated in the Rails Guides, scoping allows you to specify commonly-used queries which can be referenced as method calls on the association objects or models. With these scopes, you can use every method such as where, joins and includes. All scope methods will return an ActiveRecord::Relation object which will allow for further methods (such as other scopes) to be called on it.

    class Article < ApplicationRecord
      scope :published, -> { where(published: true) }
    end
  2. What is a sweeper?

    From Action Controller Sweeper in rails-observers.

    Sweepers are the terminators of the caching world and responsible for expiring caches when model objects change. They do this by being half-observers, half-filters and implementing callbacks for both roles. A Sweeper example:

    class ListSweeper < ActionController::Caching::Sweeper
      observe List, Item
    
      def after_save(record)
        list = record.is_a?(List) ? record : record.list
        expire_page(:controller => "lists", :action => %w( show public feed ), :id => list.id)
        expire_action(:controller => "lists", :action => "all")
        list.shares.each { |share| expire_page(:controller => "lists", :action => "show", :id => share.url_key) }
      end
    end

    The sweeper is assigned in the controllers that wish to have its job performed using the cache_sweeper class method:

    class ListsController < ApplicationController
      caches_action :index, :show, :public, :feed
      cache_sweeper :list_sweeper, :only => [ :edit, :destroy, :share ]
    end
  3. What is an observer?

    From Action Record Observer in rails-observers.

    Observer classes respond to life cycle callbacks to implement trigger-like behavior outside the original class. This is a great way to reduce the clutter that normally comes when the model class is burdened with functionality that doesn't pertain to the core responsibility of the class. Observers are put in app/models (e.g. app/models/comment_observer.rb). Example:

    class CommentObserver < ActiveRecord::Observer
      def after_save(comment)
        Notifications.comment("admin@do.com", "New comment was posted", comment).deliver
      end
    end

    This Observer sends an email when a Comment#save is finished.

  4. What is a filter and when is it called?

    From Rails Guides.

    Filters are methods that are run "before", "after" or "around" a controller action.

    Filters are inherited, so if you set a filter on ApplicationController, it will be run on every controller in your application.

    class ApplicationController < ActionController::Base
      before_action :require_login
    
      private
    
      def require_login
        ...
      end
    end
  5. How do you implement caching in Rails?

    See Rails Guides: Caching with Rails: An Overview.

  6. How do you create a REST API for your app?

  7. What is a polymorphic association and how do you implement it?

    From Rails Guides.

    With polymorphic associations, a model can belong to more than one other model, on a single association. For example, you might have a picture model that belongs to either an employee model or a product model.

    class Picture < ApplicationRecord
      belongs_to :imageable, polymorphic: true
    end
    
    class Employee < ApplicationRecord
      has_many :pictures, as: :imageable
    end
    
    class Product < ApplicationRecord
      has_many :pictures, as: :imageable
    end
  8. What is fields_for used for?

    From the apidock Rails.

    Creates a scope around a specific model object like form_for, but doesn’t create the form tags themselves. This makes fields_for suitable for specifying additional model objects in the same form.

    <%= form_for @person do |person_form| %>
      First name: <%= person_form.text_field :first_name %>
      Last name : <%= person_form.text_field :last_name %>
    
      <%= fields_for :permission, @person.permission do |permission_fields| %>
        Admin?  : <%= permission_fields.check_box :admin %>
      <% end %>
    
      <%= person_form.submit %>
    <% end %>

Related

  1. What is the difference between functional testing and unit testing?

    From Stackoverflow "Unit tests vs Functional tests".

    Unit Test - testing an individual unit, such as a method (function) in a class, with all dependencies mocked up.

    Functional Test - AKA Integration Test, testing a slice of functionality in a system. This will test many methods and may interact with dependencies like Databases or Web Services.

  2. Do you have experience using a mocking framework?

  3. Do you have any experience with BDD using RSpec or Cucumber?

  4. What plugin would you suggest for full-text search?

    Note: Not plugins.

  5. What is your most used plugin for user authorization?

  6. How do you create a plugin?

    See Rails Guides.

    rails plugin new yaffle

About

Answers for Interview Questions suggested by DAXX (2014)

License:MIT License