Ruby Developer

  Home  Web Development  Ruby Developer


“Ruby Developer Frequently Asked Questions in various Ruby Developer job interviews by interviewer. The set of questions are here to ensures that you offer a perfect answer posed to you. So get preparation for your new job interview”



74 Ruby Developer Questions And Answers

41⟩ 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.

 150 views

42⟩ 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.

 148 views

43⟩ 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.

 132 views

45⟩ 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.

 151 views

46⟩ 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.

 222 views

47⟩ 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

 133 views

48⟩ 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.

 126 views

50⟩ 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.

 126 views

51⟩ 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

 138 views

53⟩ 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.

 152 views

54⟩ 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.

 127 views

55⟩ 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.

 133 views

56⟩ 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.

 130 views

57⟩ 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.

 123 views

58⟩ 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

 118 views

60⟩ 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

 188 views