Ruby On Rails Interview Questions and Answers
Freshers / Beginner level questions & answers
Ques 1. What is Ruby On Rails?
Ques 2. Why Ruby on Rails?
1.CRUD (convention over configuration)
2. DRY Principal (Do not repeat Your self )
3. Gems and Plugins
4. Pure OOP Concept
5. Scaffolding
6.. Rest Support
7.Action Mailer
8. Rake support
9. open source
10.Rpsec Suppot for testing
Ques 3. Explain how (almost) everything is an object in Ruby.
This is a simple question based on complex concept. Here’s your chance to show off your theoretical knowledge and demonstrate that you can have an in depth conversation on class hierarchies, inheritance, methods, encapsulation, polymorphism, and more.
Explaining this could take an hour or a few minutes – there’s no single correct answer here, save from being able to demonstrate your familiarity with OOP concepts.
Ques 4. What’s your favorite testing tool?
The specific answer here is probably not important in and of itself – What’s important is that you can demonstrate familiarity with at least several testing tools, and be able to discuss their individual advantages and weaknesses. Never ventured outside of Rails default testing tools? Take some time to familiarize yourself with tools such as Rspec, FactoryGirl, Capybara, and Cucumber.
Ques 5. What are Gems and which are some of your favorites?
Gems are packaged bits of Ruby code that you can install to extend or add functionality to your app.
Be sure to be able to discuss a list of your favorite gems, why you like them, and any customizations you like to add. This is also a good opportunity to highlight any gems you may have published.
Ques 6. What is a class?
You should easily be able to explain not only what a class is, but how and when you would create a new one as well as what functionality it would provide in the larger context of your program.
Ques 7. What is the difference between a class and a module?
The straightforward answer: A module cannot be subclassed or instantiated, and modules can implement mixins.
Ques 8. What is an object?
Textbook answer here is that an object is an instance of a class and has state, behavior, and identity. In a plain text example, you can say that a truck and a car are both objects of the class Vehicle, or that apple and pear are both objects of the class Fruit.
Ques 9. How would you declare and use a constructor in Ruby?
Constructors are declared via the initialize method and get called when you call on a new object to be created.
Ques 10. How does a symbol differ from a string?
Symbols are immutable and reusable, retaining the same object_id.
Ques 11. How and when would you declare a Global Variable?
Global variables are declared with the ‘$’ symbol and can be declared and used anywhere within your program. You should use them sparingly to never.
Ques 12. How would you create getter and setter methods in Ruby?
Setter and getter methods in Ruby are generated with the attr_accessor method. attr_accessor is used to generate instance variables for data that’s not stored in your database column.
Ques 13. Describe the difference between class and instance variables?
Class variables are created with the prefix ‘@@’ and are shared by all objects in a class.
Instance variables are created with the prefix ‘@’ and belong to a single object within a class.
Ques 14. Example some of the looping structures available in Ruby?
For loop, While loop, Until Loop.
Ques 15. What is a lambda?
Lambdas are very similar to procs in terms of functionality. However, they have a few key differences. Lambdas check the number of arguments passed and will return an error if you try to pass the wrong number (while procs set extra variables to nil). The other difference is that lambdas can handle a return function, whereas procs will return an error.
Ques 16. What are Class Variables? How to define them?
Class variables are created using the @@ prefix to denote the variable as class level.
It works just like any other variable, however in the case of inheritance it works more like a static variable that is accessed across all variable instances.
Another example can be found here:
class DemoClass @@my_var = nil def initialize @@my_var = "hello world" end def my_var puts @@my_var end end class Demo2Class < DemoClass def initialize @@my_var = "goodbye world" end end demo1 = DemoClass.new demo1.my_var demo2 = Demo2Class.new demo2.my_var demo1.my_var The output would be as shown below: hello world goodbye world goodbye world
Ques 17. How to define Instance variables?
Instance variables are defined using single @ symbol.
@foo = "Hello"
Within a class they can be declared as below:
class Animal attr_accessor :name, :age end
Next you can query an object instance to find which instance variables it has.
anim = Animal.new anim.instance_variables => [ ] anim.name="John" anim.age = 3 => [:@age, :@name]
In the above case we did not put the @ symbol before the instance variables but it is implied.
Ques 18. What is the difference between ‘&&’, ‘AND’ and ‘&’ operators?
The ‘&&’ and ‘and’ are both logical and statements. They ‘&&’ operator has higher precedence though. Here’s an example of illustrate this in more detail:
foo = 3 bar = nil a = foo and bar # => nil a # => 3 a = foo && bar # => nil a # => nil
Ques 19. Does Ruby support Multiple Inheritence?
Ruby does not support multiple inheritance.
Ques 20. How can you define a Constant in Ruby on Rails?
Create a new file as shown below under: config/initializers/my_constants.rb
COLORS = ['white', 'red', 'green']
Ques 21. What is the default access modifier for a method?
By default all methods are public, except the initialize(constructor) method.
You can make methods private using this declaration within your class:
class MyClass
def method_public_here
end
PRIVATE# all methods that follow will be made private: not accessible for outside objects
def method_private_here
end
end
Ques 22. Define the Rails MVC implementation with an example.
The MVC framework is an age-old architecture pattern that works very well for most applications. Rails has adopted the MVC pattern in its inherent design.
Stated Simply:
a) Model — is where the data is — the database
b) Controller — is where the logic is for the application
c) View — is where the data is used to display to the user
Most helpful rated by users:
- What is Ruby On Rails?
- Why Ruby on Rails?
- Explain how (almost) everything is an object in Ruby.
- What are Gems and which are some of your favorites?
- How would you declare and use a constructor in Ruby?