Top 50 Ruby on rails interview questions and answers

Ruby on Rails

Table of Contents

1) Explain what is Ruby on Rails?

Ruby: It is an object-oriented programming language inspired by PERL, Smalltalk, Eiffel, Ada, Basic, and Lisp.
Rails: It is a framework used for building web application

2) Explain what is class libraries in Ruby?

Class libraries in Ruby consist of a variety of domains, such as data types, thread programming, various domains, etc.

4) Explain what is “Yield” in Ruby on Rails?

A Ruby method that receives a code block invokes it by calling it with the “Yield”.

5) Explain 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.

6) Mention what the difference is between false and nil in Ruby?

In Ruby, false is an object of FalseClass to represent a boolean value, while Nil is an object of NilClass which indicates the absence of a value. It has an object_id 4.

7) Explain what is the role of sub-directory app/controllers and app/helpers?

App/controllers: A web request from the user is handled by the Controller. The controller sub-directory is where Rails looks to find controller classes
App/helpers: The helper’s sub-directory holds any helper classes used to assist the view, model and controller classes.

8) Mention 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.

9) Explain how Symbol is different from variables?

Symbol is different from variables in following aspects

It is more like a string than variable
In Ruby string is mutable but a Symbol is immutable
Only one copy of the symbol requires to be created
Symbols are often used as the corresponding to enums in Ruby

10) Mention 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.

11) Mention what is the command to create a migration?

To create migration command includes

C:\ruby\application>ruby script/generate migration table_name

12) Explain 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.

13) Mention what is the difference between Active support’s “HashWithIndifferent” and Ruby’s “Hash”?

The Hash class in Ruby’s core library returns value by using a standard “= =” comparison on the keys. It means that the value stored for a symbol key cannot be retrieved using the equivalent string. While the HashWithIndifferentAccess treats Symbol keys and String keys as equivalent.

14) Explain 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.

15) Explain 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 — $

16) Explain how you can run Rails application without creating databases?

You can execute your application by uncommenting the line in environment.rb

path=> rootpath conf/environment.rb

config.frameworks = [ action_web_service, :action_mailer, :active_record]

17) Explain what is rake in Rails?

Rake is a Ruby Make; it is a Ruby utility that substitutes the Unix utility ‘make’, and uses a ‘Rakefile’ and ‘.rake files’ to build up a list of tasks. In Rails, Rake is used for normal administration tasks like migrating the database through scripts, loading a schema into the database, etc.

18) Explain 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.

19) Mention 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.

20) Mention 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

21) Mention 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.

22) What exactly do you know about Ruby on Rails and how well it can be defined?

It is a server-side framework for web applications that is widely regarded as best in every aspect. It is a combination of python and therefore it is easy to use. Also, there are various abilities of Smalltalk, as well as Perl which have been combined in this framework which makes it one of the trusted approaches. It is basically a high-level programming language with a wide scope in the present time and being an object-oriented approach, it has widely been considered for building large-scale projects.

23) What exactly is the role of Active Record in Ruby?

Its main aim is to make sure that the relation between the object and the database is maintained and all the validations can be considered on time.

24) What do you understand with the ERB in Ruby on Rails?

This web application framework offers users a program which is called ERB. It actually stands for Embedded Ruby. This can be considered when it comes to putting the codes that are associated to Ruby into the HTML files directly. Even when they are complex, the users have nothing to worry about and this is the best thing about this program.

25) What are the basic conditions that a user should pay attention to while preparing an ERB document in Ruby?

It is necessary to keep the code between <%and>% for its proper execution in the Ruby environment. The same should be placed between <%+=and%> in case the same need to be printed.

26) Is it possible in Ruby to develop database-driven applications in Ruby Tell anyone the basic approach through which the same can be developed in ruby?

This can be done with the help of the model view controller pattern in Ruby. Also, the users can easily make sure of a code that is not very bulky when it comes to developing the same.

27) What is the best thing which you find about Ruby on Rail so far?

It widely supports Metaprogramming and unlike other frameworks, all the codes can be written through this approach very easily and without taking the other programs which are complex into consideration. Also, the tasks which are heavy can be accomplished with this approach in no time which is one of the best things in the ruby.

28) Tell something about Action Controller in Ruby on Rails?

This is basically an important feature in Ruby which makes sure that the traffic is to be directed on one hand and the querying of the models can be done in a reliable manner. It also organizes and specifies data into a form that is suitable all over the domain.

29) What exactly do you know about the Active Records Rail?

It is basically a sub-section of Ruby and the version is very useful for handling some very essential tasks in the columns in the database. The column can be used again for handling other important tasks very easily.

30) What exactly are Harnesses and Fixtures in the Ruby?

These are basically the supporting codes with the help of which the users can easily write and can run the test cases. With the help of a rake, the users can then simply proceed with the automated tests.

31) Name the three default environments in Rails?

These are

  1. Development
  2. Production
  3. Testing

32) What exactly are the pre-software requirements for developing a project with Ruby on Rails?

There should be a database system installed on the system and a webserver to store and control all the activities and concerning programs. Also, the Rails framework and installation of Ruby should be done in a reliable manner. The users can go ahead with the WeBrick server which comes with this framework. Generally, there are other options as well that most of the users often go with and they are Apache and TPD.

33) While writing the Rails Application, what are the tasks to which you need to pay attention?

There are actually three important tasks that the users should be careful about and they are:

The very first thing is to describe as well as model the application domain. Next is to specify what can actually happen in the domain and what the critical factors to pay attention to are. The user must also pay a close attention to the views of the design available from those who have already considered the same

34) What is the MVC principle stands for in Ruby and what are the subsystems in which it divides the work of an application?

MVC stands for Model View Controller. The subsystems in which it divides the work of an application are:

  1. Active Record
  2. Action Controller
  3. Action View

35) Name a few Directories in ruby and explain their purpose?

App - It is basically responsible for organizing the components of your application. It is generally loaded with all the subdirectories that are useful
App View - these Views holds the exact display templates and fill them with the data that belong to a different framework

36) Tell anyone basic factor which is necessary to be followed when it comes to creating the rails application?

Simply create a database on PostgreSQL for holding your data on the server. It would also be good if you create a basic skeleton or a clone of the application in the initial stage.

37) Where one should look for the controller classes for handling the web request in Ruby?

The same can be located in the controller Subdirectory

38) What exactly class libraries in ruby have in them?

They generally consist of domains like thread programming, data types, as well as other useful domains which are relevant in the development.

39) What is the object relationship model in Ruby on Rails?

The same indicates clearly that all the classes are mapped to the systems in the shortest possible time and the best part is the users are free to keep up the pace in the long run. It also represents that the classes are mapped to the rows and the tables.

40) What is Garbage collection in ruby on Rails?

It simply makes sure that the pointer values should be removed reliably which is left behind after the execution of a program takes place. Also, the users can relax on tracking the objects again and again in the long run. All the objects which are no longer required can directly be moved from the memory with this approach.

41) What do you mean by the term Rail Migration?

It is basically an approach with the help of which the users can make the changes to the already existing database Schema in Ruby and can implement a version control system. The main aim is to synchronize the objects to get quality outcomes.

42) What is Ruby’s programming language?

Ruby is a dynamic, reflective, general purpose, open-source programming language that focuses on simplicity and productivity. Ruby has a mixed features of Perl, small talk, Eiffel, Ada, and Lisp. Ruby was designed to create a new language which makes a balance with the functionality of Imperative languages.

43) Why Ruby is known as a language of flexibility?

Ruby is known as a language of flexibility because it facilitates its author to alter the programming elements. Some specific parts of the language can be removed or redefined. Ruby does not restrict the user. For example, to add two numbers, Ruby allows to use + sign or the word ‘plus’. This alteration can be done with Ruby’s built-in class Numeric.

44) What are class libraries in Ruby?

Ruby class libraries contain variety of domain such as thread programming, data types, various domains. Following is a list of domains which has relevant class libraries:

Text processing
CGI Programming
Network programming
GUI programming
XML programming

45) What is the use of load and require in Ruby?

In Ruby, load and require both are used for loading the available code into the current code. In cases where loading the code required every time when changed or every times someone hits the URL, it is suggested to use ‘load’.

It case of autoload, it is suggested to use ‘require’.

46) Explain case statement in Ruby.

In Ruby, we use ‘case’ instead of ‘switch’ and ‘when’ instead of ‘case’. The case statement matches one statement with multiple conditions just like a switch statement in other languages.

47) Explain for loop in Ruby.

Ruby for loop iterates over a specific range of numbers. Hence, for loop is used if a program has fixed number of itrerations.

Ruby for loop will execute once for each element in expression.

48) Explain do while loop in Ruby.

Ruby do while loop iterates a part of program several times. In this, loop will execute at least once because do while condition is written at the end.

49) Explain until loop in Ruby.

Ruby until loop runs until the given condition evaluates to true. It exits the loop when condition becomes true. It is opposite of the while loop.

50) Explain break statement in Ruby.

Ruby break statement is used to terminate a loop. It is mostly used in while loop, where value is printed till the condition, is true.

Rajesh Kumar
Follow me
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x