Top Ruby And Rails Interview Questions
1.Explain about ruby names
Classes,variables,methods,constants and modules can be referred by ruby names.When you want to distinguish between various names you can specify that by the first character of the name.
Some of the names are used as reserve words which should not be used for any other purpose.A name can be lowercase letter,upper case letter,number,or an underscore,make sure that you follow the name by name characters.
2.What is Session and Cookies?
Session:
are used to store user information on the server side.cookies: are used to store information on the browser side or we can say client side
Session :
say session[:user] = “Alex” it remains when the browser is not closed.
3.What is request.xhr?
A request.xhr tells the controller that the new Ajax request has come,It always return Boolean values (TRUE or FALSE).
4.What is MVC? and how it Works?
MVC tends for Model-View-Controller,used by many languages like PHP,Perl,Python etc.The flow goes like this: Request first comes to the controller,controller finds and appropriate view and interacts with model,model interacts with your database and send the response to controller then controller based on the response give the output parameter to view,
for Example your url is something like this: http://localhost:3000/users/new here users is your controller and new is your method,there must be a file in your views/users folder named new.html.erb,so once the submit button is pressed,User model or whatever defined in the rhtml form_for syntax,will be called and values will be stored into the database.
5.What are the things we can define in the model?
There are lot of things you can define in models few are:
1.Validations (like validates_presence_of,numeracility_of,format_of etc.)
2.Relationships(like has_one,has_many,HABTM etc.)
3.Callbacks(like before_save,after_save,before_create etc.)
4.Suppose you installed a plugin say validation_group,So you can also define validation_group settings in your model
5.ROR Queries in Sql
6.Active record Associations Relationship
6.What is ORM in Rails?
ORM tends for Object-Relationship-Model,it means that your Classes are mapped to table in the database,and Objects are directly mapped to the rows in the table.
7.How many Types of Associations Relationships does a Model has?
When you have more than one model in your rails application,you would need to create connection between those models.You can do this via associations.
Active Record supports three types of associations:
1.one-to-one :
A one-to-one relationship exists when one item has exactly one of another item.For example,a person has exactly one birthday or a dog has exactly one owner.
2.one-to-many :
A one-to-many relationship exists when a single object can be a member of many other objects.For instance,one subject can have many books.
3.many-to-many :
A many-to-many relationship exists when the first object is related to one or more of a second object,and the second object is related to one or many of the first object.You indicate these associations by adding declarations to your models: has_one,has_many,belongs_to,and has_and_belongs_to_many.
8.What is the Difference between Static and Dynamic Scaffolding?
The Syntax of Static Scaffold is like this: ruby script/generate scaffold User Comment.Where Comment is the model and User is your controller,So all n all static scaffold takes 2 parameter i.e your controller name and model name,whereas in dynamic scaffolding you have to define controller and model one by one.
9.How you run your Rails Application without creating database?
MYou can run application by uncomment the line in environment.rb
Path => rootpath conf/ environment.rb
# Skip frameworks you’re not going to use (only works if using vendor/rails)
config.frameworks -= [ :action_web_service,:action_mailer,:active_record ].
10.What are helpers and how to use helpers in ROR?
Helpers (“view helpers”) are modules that provide methods which are automatically usable in your view.They provide shortcuts to commonly used display code and a way for you to keep the programming out of your views.The purpose of a helper is to simplify the view.It’s best if the view file (RHTML/RXML) is short and sweet,so you can see the structure of the output.
11.What is Active Record?
Active Record are like Object Relational Mapping(ORM),where classes are mapped to table ,objects are mapped to columns and object attributes are mapped to data in the table
12.Ruby Support Single Inheritance/Multiple Inheritance or Both?
SRuby Supports only Single Inheritance.You can achieve Multiple Inheritance through MIXIN concept means you achieve using module by including it with classes.
13.How many types of callbacks available in ROR?
* (-) save
* (-) valid
* (1) before_validation
* (2) before_validation_on_create
* (-) validate
* (-) validate_on_create
* (3) after_validation
* (4) after_validation_on_create
* (5) before_save
* (6) before_create
* (-) create
* (7) after_create
* (8) after_save
14.What can Rails migration do?
1. create_table(name,options)
2. drop_table(name)
3. rename_table(old_name,new_name)
4. add_column(table_name,column_name,type,options)
5. rename_column(table_name,column_name,new_column_name)
6. change_column(table_name,column_name,type,options)
7. remove_column(table_name,column_name)
8. add_index(table_name,column_name,index_type)
9. remove_index(table_name,column_name) Migrations support all the basic data types:
string,text,integer,float,datetime,timestamp,time,date,binary and boolean:
1. string – is for small data types such as a title.
2. text – is for longer pieces of textual data,such as the description.
3. integer- is for whole numbers.
4. float- is for decimals.
5. datetime and timestamp – store the date and time into a column.
6. date and time – store either the date only or time only.
7. binary – is for storing data such as images,audio,or movies.
8. boolean – is for storing true or false values.Valid column options are:
1. limit ( :limit => “50” )
2. default (:default => “blah” )
3. null (:null => false implies NOT NULL)
15.What is the naming conventions for methods that return a boolean result?
Methods that return a boolean result are typically named with a ending question mark.
For example:
def active? return true #just always returning true end.
16.How do the following methods differ: @my_string.strip and @my_string.strip!?
The strip! method modifies the variable directly.Calling strip (without the !) returns a copy of the variable with the modifications,the original variable is not altered.
17.What’s the difference in scope for these two variables: @name and @@name?
@name is an instance variable and @@name is a class variable.
18.What is the log that has to see to check for an error in ruby rails?
Rails will report errors from Apache in log/apache.log and errors from the Ruby code in log/development.log.If you’re having a problem,do have a look at what these logs are saying.On Unix and Mac OS X you may run tail -f log/development.log in a separate terminal to monitor your application’s execution.
19.What is the use of global variable $ in Ruby?
A class variable starts with an @@ sign which is immediately followed by upper or lower case letter.You can also put some name characters after the letters which stand to be a pure optional.A class variable can be shared among all the objects of a class.
A single copy of a class variable exists for each and every given class.To write a global variable you start the variable with a $ sign which should be followed by a name character.Ruby defines a number of global variables which also include other punctuation characters such as $_ and $-k.
20.Where does the start_tabnav gets informations for tabs rendering in ruby rail?
The main Symbol let the start_tabnav method known to look for a special MainTabnav class where all are happens.
21.What is the Install rail package?
There are several packages that you can download and install.The prebuilt Rails installer called Install rail which currently is only for Windows.
22.What is the log that has to see to check for an error in ruby rails?
Rails will report errors from Apache in log/apache.log and errors from the Ruby code in log/development.log.If you’re having a problem,do have a look at what these logs are saying.On Unix and Mac OS X you may run tail -f log/development.log in a separate terminal to monitor your application’s execution.
23.What is the use of super in ruby rails?
Ruby uses the super keyword to call the superclass (Parent class) implementation of the current method
24.What is the difference between nil and false in ruby?
False is a boolean datatype,Nil is not a data type it have object_id 4
25.What is the use of load,require,auto_load,require_relative in Ruby?
A method that loads and processes the Ruby code from a separate file,including whatever classes,modules,methods,and constants are in that file into the current scope.
Load is similar,but rather than performing the inclusion operation once,it reprocesses the code every time load is called.auto_load – Whenever the interpreter call the method that time only it will initiate the method in hat file.require_relative – It it to load local folder files.