Ruby On Rails Interview Questions

Ruby On Rails Interview Question And Answers:

Q.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.

Q.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

Q.What are Gems and which are some of your favorites?

  • Short answer: 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

Q.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.

Q.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.
  • Be prepared to discuss what this actually means in real life, and when you would use a module vs. a class and why.

Q.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.
  • You should be able to explain in detail how object structure and behavior relate to their common class, and why this is so important in Ruby (see question 1).

Q.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.

Using the code snippet below, calling Order.new acts as a constructor for an object of the class Order

Initializing instance variables of the class Order

Q.How does a symbol differ from a string?

  • Short answer: symbols are immutable and reusable, retaining the same object_id.
  • Be prepared to discuss the benefits of using symbols vs. strings, the effect on memory usage, and in which situations you would use one over the other.

Q.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.

Q.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.
  • You can also take the long route and create them manually.

Q.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.
  • Beyond the simple textbook definition, be able to describe an example of a class and how you would use class and instance variables within it, and how they relate to issues of class inheritance.

Q.Explain some of the looping structures available in Ruby?

  • For loop, While loop, Until Loop.
  • Be able to explain situations in which you would use one over another

Q.Explain the difference between a has_one and belongs_to association:

  • has_one:Indicates a direct 1:1 relationship between objects where each instance of a model contains one instance of another model.
  • A product has_one provider, a customer has_one order

A has_one association schematic, from Rubyonrails.org

  • belongs_to: Represents the inverse of a has_one (or has_many) association.
  • ex) An order belongs_to  a customer

A belongs_to association schematic, from Rubyonrails.org

  • A good way to remember this is that if a table has foreign keys, its model should have a belongs_to association.
  • Be able to explain the difference and describe an example of how you would assign these associations to two related models.

Q.Explain a polymorphic association:

Polymorphic associations allow a model to belong to more than one other model through a single association.

A polymorphic association schematic, from Rubyonrails.org

Model structures of a polymorphic association, in Ruby code, from Ruybonrails.org

  • Here, the class Picture belongs_to both Employee and Product, but does so through a single association rather than through multiple.
  • Be sure to know an appropriate situation to create a polymorphic association, such as creating a comment model associated with multiple other models (articles, photos, etc.). The advantage of using polymorphism here is that it allows you to create a single comment model, rather than separate models for each one (PhotoComment model, ArticleComment model, etc.)

Q.What is a Proc?

  • Procs, short for procedures, act similar to blocks, but can be saved as variables and reused. Think of them as blocks you can call over and over again on multiple arrays.
  • Explain an instance when you would use a proc over a block. Read more here

Q.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.Read more here

Q.What are the three levels of method access control for classes and what do they signify? What do they imply about the method?

  • Public, protected, and private.
  • Public methods can be called by all objects and subclasses of the class in which they are defined in.
  • Protected methods are only accessible to objects within the same class.
  • Private methods are only accessible within the same instance.
  • Be able to explain why this does (or doesn’t matter), and when you would want to set a method as private.

Q.Explain what functional testing is?

  • Functional testing in Rails allows you to test the response of  various actions contained in a controller. Using the Rails default test library, mini test, functional tests use a collection of assert statements that will tell your testing library to expect a certain response based on a control request passed in (either a get, post, patch, put, head, delete request).
  • The two example tests below show functional tests for making sure the post and delete requests in our UsersController properly create and destroy users. The functional tests do this by making sure the requests result in a change in the User.count and that they then redirect to the desired pages

Functional tests for the create and destroy methods in a User model.






0 Responses on Ruby On Rails Interview Questions"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.