1. Tell me how can you declare a block in Ruby?

In Ruby, the code in the block is always enclosed within braces ({}). You can invoke a block by using “yield statement”.

2. Do you know about Dig, Float and Max?

☛ Float class is used whenever the function changes constantly.
☛ Dig is used whenever you want to represent a float in decimal digits.
☛ Max is used whenever there is a huge need of Float.

3. Explain me what is the purpose of RJs in Rails?

RJs is a template that produces JavaScript which is run in an eval block by the browser in response to an AJAX request. It is sometimes used to define the JavaScript, Prototype and helpers provided by Rails.

4. Tell me what is Mixin in Rails?

Mixin in Ruby offers an alternative to multiple inheritances, using mixin modules can be imported inside other class.

5. What is interpolation in Ruby?

Ruby Interpolation is the process of inserting a string into a literal. By placing a Hash (#) within {} open and close brackets, one can interpolate a string into the literal.

6. Tell us the types of variables available in Ruby Class?

Types of variables available in Ruby Class are,

☛ Local Variables
☛ Global Variables
☛ Class Variables
☛ Instance Variables

7. Tell me what is Rails Migration?

Rails Migration enables Ruby to make changes to the database schema, making it possible to use a version control system to leave things synchronized with the actual code.

8. Do you know when self.up and self.down method is used?

When migrating to a new version, self.up method is used while self.down method is used to roll back my changes if needed.

9. Tell me what is Rails Active Record in Ruby on Rails?

Rails active record is the Object/Relational Mapping (ORM) layer supplied with Rails. It follows the standard ORM model as

☛ Table map to classes
☛ Rows map to objects
☛ Columns map to object attributes

10. Explain me the log that has to be seen to report errors in Ruby Rails?

Rails will report errors from Apache in the log/Apache.log and errors from the Ruby code in log/development.log.

11. What is the difference between a single quote and double quote?

A single-quoted strings don't process ASCII escape codes, and they don't do string interpolation.

12. Tell me how you can list all routes for an application?

To list out all routes for an application you can write rake routes in the terminal.

13. Can you list out the few features of Ruby?

☛ Free format – You can start writing from program from any line and column
☛ Case sensitive – The uppercase and lowercase letters are distinct
☛ Comments – Anything followed by an unquoted #, to the end of the line on which it appears, is ignored by the interpreter
☛ Statement delimiters- Multiple statements on one line must be separated by semicolons, but they are not required at the end of a line.

14. Please explain the three levels of access control for Ruby methods?

In Ruby, methods may either be public, protected, or private. Public methods can be called by anyone. Protected methods are only accessible within their defining class and its subclasses. Private methods can only be accessed and viewed within their defining class.

15. Tell me what is the function of garbage collection in Ruby on Rails?

The functions of garbage collection in Ruby on Rails includes

☛ It enables the removal of the pointer values which 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
☛ It gives the advantage of removing the inaccessible objects from the memory, and allows other processes to use the memory

16. Can you please explain what is the difference between String and Symbol?

They both act in the same way only they differ in their behaviors which are opposite to each other. The difference lies in the object_id, memory and process tune when they are used together. Symbol belongs to the category of immutable objects whereas Strings are considered as mutable objects.

17. Explain me what is the role of Rails Controller?

The Rails controller is the logical center of the application. It faciliates the interaction between the users, views, and the model. It also performs other activities like

☛ It is capable of routing external requests to internal actions. It handles URL extremely well
☛ It regulates helper modules, which extend the capabilities of the view templates without bulking of their code
☛ It regulates sessions; that gives users the impression of an ongoing interaction with our applications

18. Tell me what is Polymorphic Association in Ruby on Rails?

Polymorphic Association allows an ActiveRecord object to be connected with Multiple ActiveRecord objects. A perfect example of Polymorphic Association is a social site where users can comment on anywhere whether it is a videos, photos, link, status updates etc. It would be not feasible if you have to create an individual comment like photos_comments, videos_comment and so on.

19. Do you know what are the types of caching used in rails, what are they?

There are primarily three types of caching used in Rails.

1. Page Caching

Page caching is a Rails mechanism which allows the request for a generated page to be fulfilled by the webserver (i.e. Apache or NGINX) without having to go through the entire Rails stack.

2. Action Caching

Page Caching cannot be used for actions that have before filters - for example, pages that require authentication. This is where Action Caching comes in. Action Caching works like Page Caching except the incoming web request hits the Rails stack so that before filters can be run on it before the cache is served. This allows authentication and other restrictions to be run while still serving the result of the output from a cached copy.

3. Fragment Caching

Dynamic web applications usually build pages with a variety of components not all of which have the same caching characteristics. When different parts of the page need to be cached and expired separately you can use Fragment Caching.

20. Tell me what is the difference between calling super and calling super()?

A call to super invokes the parent method with the same arguments that were passed to the child method. An error will therefore occur if the arguments passed to the child method don't match what the parent is expecting.

A call to super() invokes the parent method without any arguments, as presumably expected. As always, being explicit in your code is a good thing.

21. What is the difference between a gem and a plugin in Ruby?

☛ Gem: A gem is a just ruby code. It is installed on a machine, and it's available for all ruby applications running on that machine.
☛ Plugin: Plugin is also ruby code, but it is installed in the application folder and only available for that specific application.

22. Tell us what is the difference between redirect and render in Ruby on Rails?

☛ Redirect is a method that is used to issue the error message in case the page is not issued or found to the browser. It tells browser to process and issue a new request.
☛ Render is a method used to make the content. Render only works when the controller is being set up properly with the variables that require to be rendered.

23. Tell me what is ORM (Object-Relationship-Model) in Rails?

ORM or Object Relationship Model in Rails indicate that your classes are mapped to the table in the database, and objects are directly mapped to the rows in the table.

24. What is the difference between Procs and Blocks?

The difference between Procs and Blocks,

☛ Block is just the part of the syntax of a method while proc has the characteristics of a block
☛ Procs are objects, blocks are not
☛ At most one block can appear in an argument list
☛ Only block is not able to be stored into a variable while Proc can

25. Tell us how would you implement hash in Ruby internally?

The purpose of a hash function is to convert a given a key into an integer of limited range. In order to reduce the range, we use a technique called the division method. In the division method, the key is divided by the size of the storage and the remainder is the location inside that table where a record can be stored.

But in real life programming the keys includes strings, objects, etc as well. This is solved by using a one-way hash function over the key and then applying the division method to get the location. The hash function is a mathematical function that takes a string of any length and produces a fixed length integer value. The hash data structure derives it's name from this hashing mechanism. Ruby uses the murmur hash function and then applies the division method with a prime number M, which Ruby determines based on the table size that is needed for storage.

26. Tell me how do you remove nil values in array using ruby?

Array#compact removes nil values.

Example:

>> [nil,123,nil,"test"].compact
=> [123, "test"]

27. Do you know how Ruby looks up a method to invoke?

Since Ruby is a pure object-oriented language, it's important to make sure your developer thoroughly understands how objects work. The first place that Ruby looks for a method is in the object's metaclass or eigenclass-the class that contains methods directly defined on the object.

If the method cannot be found in an object's metaclass, Ruby will then search for the method in the ancestors of an object's class. The list of ancestors for any class starts with the class of the object itself, and climbs parent classes until it reaches the Object, Kernel, and BasicObject classes at the top of the Ruby class hierarchy.

If Ruby cannot find the method, it will internally send another method aptly called “method_missing?” to the object class. Ruby will repeat another search for this method, and will at least find it in the object class, provided the programmer did not see fit to define the “method_missing?” class earlier in the ancestry of the object.

28. What is a class library in Ruby?

Ruby class libraries consist of a variety of domains, such as thread programming, data types, various domains, etc. These classes give flexible capabilities at a high level of abstraction, giving you the ability to create powerful Ruby scripts useful in a variety of problem domains. The following domains which have relevant class libraries are,

☛ GUI programming
☛ Network programming
☛ CGI Programming
☛ Text processing

29. Please explain what are the positive aspects of Rails?

Rails provides many features like

☛ Meta-programming: Rails uses code generation but for heavy lifting it relies on meta-programming. Ruby is considered as one of the best language for Meta-programming.
☛ Active Record: It saves object to the database through Active Record Framework. The Rails version of Active Record identifies the column in a schema and automatically binds them to your domain objects using metaprogramming
☛ Scaffolding: Rails have an ability to create scaffolding or temporary code automatically
☛ Convention over configuration: Unlike other development framework, Rails does not require much configuration, if you follow the naming convention carefully
☛ Three environments: Rails comes with three default environment testing, development, and production.
☛ Built-in-testing: It supports code called harness and fixtures that make test cases to write and execute.

30. Do you know what is Cross-Site Request Forgery (CSRF) and how Rails is protected against it?

CSRF is a form of attack where hacker submits a page request on your behalf to a different website, causing damage or revealing your sensitive data. To protect from CSRF attacks, you have to add “protect_from_forgery” to your ApplicationController. This will cause Rails to require a CSRF token to process the request. CSRF token is given as a hidden field in every form created using Rails form builders.

31. Tell me how you define Instance Variable, Global Variable and Class Variable in Ruby?

☛ Ruby Instance variable begins with - @
☛ Ruby Class variables begin with - @@
☛ Ruby Global variables begin with - $

32. Tell me in Ruby code, often it is observed that coder uses a short hand form of using an expression like array.map(&:method_name) instead of array.map { |element| element.method_name }. How this trick actually works?

When a parameter is passed with “&” in front of it. Ruby will call to_proc on it in an attempt to make it usable as a block. So, symbol to_Proc will invoke the method of the corresponding name on whatever is passed to it. Thus helping our shorthand trick to work.

33. Please explain the role of thread pooling in relation to the thread lifecycle in Ruby?

In Ruby, the lifecycle of a single thread starts automatically as soon as CPU resources are available. The thread runs the code in the block where it was instantiated and obtains the value of the last expression in that block and returns it upon completion. Threads use up resources, but running multiple threads at a time can improve an app's performance.

Thread pooling is a technique wherein multiple pre-instantiated reusable threads are left on standby, ready to perform work when needed. Thread pooling is best used when there are a large number of short tasks that must be performed. This avoids the overhead of having to create a new thread every time a small task is about to be performed.

34. Explain me what is the difference between concern and application_controller.rb in ruby on rails?

Concerns are basically modules that get mixed into controller or model classes for instance. A Concern extends ActiveSupport::Concern module. It helps slim down the model/controller classes, and makes it easier to reuse common code across multiple model/controller classes. Concerns are like helper modules. We can define helper methods in concerns and can include or extend those concerns from various controller/model classes to sharing and DRY up the model/controller code.

On the other hand, ApplicationController is a controller class (NOT a module) and in your Rails application, it is the base class from which all of your other controllers classes are derived from. e.g.

class FooController < ApplicationController
. . .
end

class BarController < ApplicationController
. . .
end
In, Model–View–Controller (MVC) software architectural pattern, a controller implements business logics, can send commands to the model to update the model's state. It can also send commands to its associated view to change the view's presentation of the model. ApplicationController is that kind where as the Concerns are like helpers to get their job done.

Additional - Using concerns/services you can put your business logic in the controller; as a controller should not be business logic heavy, its main work is to get the request and get the response out.

35. Tell me how would you freeze an object in Ruby? Can you provide a simple example?

Sometimes it can be useful to prevent an object from being changed. This can be accomplished using the freeze method (Object.freeze) as in the sample code below.

water.freeze
if( water.frozen? )
puts "Water object is a frozen object"
else
puts "Water object is a normal object"
end