Top 51 Ruby On Rails Interview Questions & Answers Flashcards ionicons-v5-c

Explain what is Ruby

It is an object oriented programming language inspired by PERL and PYTHON

Explain what is Ruby on Rails

It is a framework used for building web application

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

What is the naming convention in Rails

Variables: For declaring variables, all letters are lowercase, and words are separated by underscoresClass and Module: Modules and Classes use MixedCase and have no underscore; each word starts with a uppercase letterDatabase Table: The database table name should have lowercase letters and underscore between words, and all table names should be in the plural form for example invoice_itemsModel: It is represented by unbroken MixedCase and always have singular with the table nameController: Controller class names are represented in plural form, such that OrdersController would be the controller for the order table

What is "yield" in Ruby on Rails?

A Ruby method that receives a code block invokes it by calling it with the "yield"

What is ORM in Rails?

ORM is Object Relational Mapping. It means you don't have to manually call the database yourself; the ORM handles it for you.Ruby on Rails uses one called ActiveRecord.ORM allows you to do things such as:User.find(50).contactsInstead of manually writing a SELECT statement with JOINs, WHEREs, etc.

What is the difference between false and nil in Ruby?

false indicates a boolean datatype and nil datatype is equivalent to null

List 6 positive aspects of Rails

meta-programming, active record, scaffolding, convention over configuration, three environments, built-in-testing

meta-programming

it is dynamic nature of the language, which allows you to define and redefine methods and classes at runtime

active record

it saves an object to the database through active record framework - rails version of active record identifies the column in a schema and automatically binds them to your domain objects using metaprogramming

convention over configuration

Convention over configuration relies on the development of a program through the use of an underlying language's native procedures, functions, classes and variables. This approach reduces or eliminates the need for additional software configuration files, ultimately facilitating and expediting software development, code consistency and maintenance.

three environments

rails come with three default environment testing, development, and production

buit-in-testing

supports code called harness and fixtures that make test . cases to write and execute

what is the role of the sub-directory app/controllers?

App/controllers: a web request from the user is handled by the controller. it is where Rails looks to find controller classes

what is the role of the sub-directory app/helpers?

App/helpers: the helper's sub-directory holds any helper classes used to assist the view, model and controller classes

what is the difference between string and symbol?

difference is the object_id, memory, and process tune. symbol belongs to the category of immutable objects where as strings are considered as mutable objects

how is symbol different from variables?

It is more like a string than variableIn Ruby string is mutable but a Symbol is immutableOnly one copy of the symbol requires to be createdSymbols are often used as the corresponding to enums in Ruby

what is Rails Active Record in Ruby on Rails

rails active record is the Object Relational Mapping layer supplied with Rails

how does Rails implement Ajax?

Rails triggers an Ajax Operation in following waysSome trigger fires: The trigger could be a user clicking on a link or button, the users inducing changes to the data in the field or on a formWeb client calls the server: A Java-script method, XMLHttpRequest, sends data linked with the trigger to an action handler on the server. The data might be the ID of a checkbox, the whole form or the text in the entry fieldServer does process: The server side action handler does something with the data and retrieves an HTML fragment to the web clientClient receives the response: The client side JavaScript, which Rails generates automatically, receives the HTML fragment and uses it to update a particular part of the current

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

List out what Rails migration can do

Rails Migration can do following thingsCreate tableDrop tableRename tableAdd columnRename columnChange columnRemove column and so on

when is self.up and self.down method used?

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

what is the role of Rails Controller?

The Rails controller is the logical center of the application. It facilitates the interaction between the users, views, and the model. It also performs other activities likeIt is capable of routing external requests to internal actions. It handles URL extremely wellIt regulates helper modules, which extend the capabilities of the view templates without bulking of their codeIt regulates sessions; that gives users the impression of an ongoing interaction with our applications

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.

what is Cross-Site Request Forgery and how is Rails 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.

What is Mixin in Rails?

research answer

How do 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 - $

what is the difference between the Observers and Callbacks in Ruby on Rails?

Rails Observers: Observers is same as Callback, but it is used when method is not directly associated to object lifecycle. Also, the observer lives longer, and it can be detached or attached at any time. For example, displaying values from a model in the UI and updating model from user input.Rails Callback: Callbacks are methods, which can be called at certain moments of an object's life cycle for example it can be called when an object is validated, created, updated, deleted, A call back is short lived. For example, running a thread and giving a call-back that is called when thread terminates

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.

what is a sweeper in Rails?

sweepers are responsible for expiring or terminating caches when model object changes

the log that reports 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.

what is the difference between dynamic and static scaffolding?

Dynamic Scaffolding It automatically creates the entire content and user interface at runtimeIt enables to generation of new, delete, edit methods for the use in applicationIt does not need a database to be synchronizedStatic ScaffoldingIt requires manual entry in the command to create the data with their fieldsIt does not require any such generation to take placeIt requires the database to be migrated

what is the function of garbage collection in Ruby on Rails?

The functions of garbage collection in Ruby on Rails includesIt enables the removal of the pointer values which is left behind when the execution of the program endsIt frees the programmer from tracking the object that is being created dynamically on runtimeIt gives the advantage of removing the inaccessible objects from the memory, and allows other processes to use the memory

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 requestRender 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

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.

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.

what are the limits of Ruby on Rails?

Ruby on Rails has been designed for creating a CRUD web application using MVC. This might make Rails not useful for other programmers. Some of the features that Rails does not support includeForeign key in databasesLinking to multiple data-base at onceSoap web servicesConnection to multiple data-base servers at once

what is the difference between calling super() and super call?

super(): 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.super call: 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.

what is 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.

how is Ruby regular expressions defined?

Ruby regular expression is a special sequence of characters that helps you match or find other strings. A regular expression literal is a pattern between arbitrary delimiters or slashes followed by %r.

what is a defined operator

Define operator states whether a passed expression is defined or not. If the expression is defined, it returns the description string and if it is not defined it returns a null value.

what are the types of variables available in Ruby class?

local variables, global variables, class variables, instance variables

how do 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".

what is the difference between puts and putc statement

Unlike the puts statement, which outputs the entire string onto the screen. The Putc statement can be used to output one character at a time.

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?

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?

what is interpolation in Ruby

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

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 blockProcs are objects, blocks are notAt most one block can appear in an argument listOnly block is not able to be stored into a variable while Proc can

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.

what is the difference between a gem and a plugin in Ruby

GEM: a gem is just ruby code. it is installed on a machine, and it's available for all ruby applications running on that machinePlugin: it ruby code, but its installed in the application folder and only available for the specific application

what is the difference between extend and include?

The "include" makes the module's methods available to the instance of a class, while "extend" makes these methods available to the class itself.