Tuesday, March 22, 2016

Ruby on Rails Interview Question

1. What is MVC 

Answer:   Model View Controller or MVC as it is popularly called, is a software design pattern for developing web applications. A Model View Controller pattern is made up of the following three parts:
  • Model - The lowest level of the pattern which is responsible for maintaining data.
  • View - This is responsible for displaying all or a portion of the data to the user.
  • Controller - Software Code that controls the interactions between the Model and View.

2. What is OOPs

AnswerObject-oriented programming (OOP) is a programming language model organized around objects rather than "actions" and data rather than logic. Historically, a program has been viewed as a logical procedure that takes input data, processes it, and produces output data.


3. What are OOPs concepts

Answer:  Abstraction: The process of picking out (abstracting) common features of objects and procedures.

Class: A category of objects. The class defines all the common properties of the different objects that belong to it.

Encapsulation: The process of combining elements to create a new entity. A procedure is a type of encapsulation because it combines a series of computer instructions.

Encapsulation reduce system complexity and increase robustness by decoupling its components.

Information hiding: The process of hiding details of an object or function. Information hiding is a powerful programming technique because it reduces complexity.

Inheritance: a feature that represents the "is a" relationship between different classes.

Usually, inheritance is used to specialize a class. See the following example :
A class can only inherit from one class as opposed to c++ where multi-inheritance can be done (not always for the better).
You can however replicate a certain form of multi-inheritance through the use of modules as mix-ins :

Interface: the languages and codes that the applications use to communicate with each other and with the hardware.

Messaging: Message passing is a form of communication used in parallel programming and object-oriented programming.

Object: a self-contained entity that consists of both data and procedures to manipulate the data.

Polymorphism: A programming language's ability to process objects differently depending on their data type or class.

Procedure: a section of a program that performs a specific task.


3. What is Module

AnswerModule 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.


4. What it the difference extend and include a module

Answer: Extend module in class allows to use methods as class methods and Include module in class allows methods as instance methods


5. What is meta-programming

AnswerMetaprogramming is the writing of computer programs with the ability to treat programs as their data. It means that a program could be designed to read, generate, analyse or transform other programs, and even modify itself while running.

In the example below, here are some of the "meta" changes we're making to the program:
  • Reopening classes: Add a method named foldl to Ruby's native Array class
  • Programmatic method invocation: Use send to call a method by name programmatically
The end result is the ability to combine the elements of any array containing any type of object in fairly arbitrary ways.
Example Code:
class Array
  def foldl(method)
    inject {|result, i| result ? result.send(method, i) : i }
puts [1000.0, 200.0, 50.0].foldl("/") # Feel free to experiment with this
Metaprogramming is supported across many languages using many different techniques; you’ve probably used some of them already. Here’s a brief list of some popular languages besides Ruby that support metaprogramming in some form.


6.  What are the types of variables in Ruby

AnswerRuby Global VariablesGlobal variables begin with $. Uninitialized global variables have the value nil and produce warnings with the -w option.

Ruby Instance VariablesInstance variables begin with @. Uninitialized instance variables have the value nil and produce warnings with the -w option.

Ruby Class VariablesClass variables begin with @@ and must be initialized before they can be used in method definitions.

Ruby Local VariablesLocal variables begin with a lowercase letter or _. The scope of a local variable ranges from class, module, def, or do to the corresponding end or from a block's opening brace to its close brace {}.

Ruby ConstantsConstants begin with an uppercase letter. Constants defined within a class or module can be accessed from within that class or module, and those defined outside a class or module can be accessed globally.


7. What is the Asset Pipeline?

Answer: The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, Sass and ERB. The asset pipeline is technically no longer a core feature of Rails 4, it has been extracted out of the framework into the sprockets-rails gem.


8. What is Agile Methodology

AnswerAgile methodology is an alternative to traditional project management, typically used in software development. It helps teams respond to unpredictability through incremental, iterative work cadences, known as sprints. Agile methodologies are an alternative to waterfall, or traditional sequential development.


9. What is Ruby

AnswerRuby is a dynamic, reflective, object-oriented, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan. According to its creator, Ruby was influenced by Perl, Smalltalk, Eiffel, Ada, and Lisp. Ruby is "an interpreted scripting language for quick and easy object-oriented programming"


10. What is SDLC

AnswerSoftware Development Life Cycle, SDLC for short, is a well-defined, structured sequence of stages in software engineering to develop the intended software product.

Types of SDLC
Iterative Model
Waterfall Model
Spiral Model


11. What Is the Difference Between a Block, a Proc, and a Lambda in Ruby?

Answer: Procs are objects, blocks are not
At most one block can appear in an argument list
Lambdas check the number of arguments, while procs do not
Lambdas and procs treat the ‘return’ keyword differently


12.  What is Rack

Answer: Rack: a Ruby Webserver Interface, Rack provides a minimal interface between webservers that support Ruby and Ruby frameworks.

You can handle an app directly:
# my_rack_app.rb
require 'rack'
app = Proc.new do |env|
    ['200', {'Content-Type' => 'text/html'}, ['A barebones rack app.']]
Rack::Handler::WEBrick.run app
Or, you can use the rackup command line tool and avoid specifying details like port and server until runtime:
# config.ru
run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']] }
Invoked like so:
$ rackup config.ru
... and you're good to go!


13.  What is a gem?


Each gem has a name, version, and platform. For example, the rake gem has a 0.8.7 version (from May, 2009). Rake’s platform is ruby, which means it works on any platform Ruby runs on.

Platforms are based on the CPU architecture, operating system type and sometimes the operating system version. Examples include “x86-mingw32” or “java”. The platform indicates the gem only works with a ruby built for the same platform. RubyGems will automatically download the correct version for your platform. See gem help platform for full details.

Inside a gems are the following components:

Code (including tests and supporting utilities)
Each gem follows the same standard structure of code organization:

% tree freewill
├── bin/
│   └── freewill
├── lib/
│   └── freewill.rb
├── test/
│   └── test_freewill.rb
├── Rakefile
└── freewill.gemspec
Here, you can see the major components of a gem:

The lib directory contains the code for the gem
The test or spec directory contains tests, depending on which test framework the developer uses
A gem usually has a Rakefile, which the rake program uses to automate tests, generate code, and perform other tasks.
This gem also includes an executable file in the bin directory, which will be loaded into the user’s PATH when the gem is installed.
Documentation is usually included in the README and inline with the code. When you install a gem, documentation is generated automatically for you. Most gems include RDoc documentation, but some use YARD docs instead.
The final piece is the gemspec, which contains information about the gem. The gem’s files, test information, platform, version number and more are all laid out here along with the author’s email and name.


14. What are callbacks in ROR?

1 The Object Life Cycle
During the normal operation of a Rails application, objects may be created, updated, and destroyed. Active Record provides hooks into this object life cycle so that you can control your application and its data.

Callbacks allow you to trigger logic before or after an alteration of an object's state.

2 Callbacks Overview
Callbacks are methods that get called at certain moments of an object's life cycle. With callbacks it is possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.


15. What are filters in ROR

Answer: Filters enable controllers to run shared pre- and post-processing code for its actions. These filters can be used to do authentication, caching, or auditing before the intended action is performed. Or to do localization or output compression after the action has been performed. Filters have access to the request, response, and all the instance variables set by other filters in the chain or by the action (in the case of after filters).

around_filter: Around filters wrap an action, executing code both before and after. They may be declared as method references, blocks, or objects responding to filter or to both before and after.


16. What are prepend_before_filter and prepend_after_filter?

Answer: Using before_filter and after_filter appends the specified filters to the existing chain. That‘s usually just fine, but some times you care more about the order in which the filters are executed. When that‘s the case, you can use prepend_before_filter and prepend_after_filter. Filters added by these methods will be put at the beginning of their respective chain and executed before the rest. For example:
  class ShoppingController < ActionController::Base
    before_filter :verify_open_shop

  class CheckoutController < ShoppingController
    prepend_before_filter :ensure_items_in_cart, :ensure_items_in_stock


17.  What are engines?

Answer: Engines can be considered miniature applications that provide functionality to their host applications. A Rails application is actually just a "supercharged" engine, with the Rails::Application class inheriting a lot of its behavior from Rails::Engine.