ruby on rails interview questions

We gathered top interview questions that will help you land the ruby on rails position you desire. Programming interviews many times include open questions that are design to see your thought process, so before answering take a few moments to gather your thoughts and try to be coherent as possible. Take the time to read the job description a few times, and see how you measure in each of the requirements. Learn about the company and it’s product online and the type of technology they use.

Take our Ruby on Rails multiple questions test to find out how you rank

Ruby on Rails Test

 

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

 

2. What is a module? Can you tell me the difference between classes and modules?

Modules serve as a mechanism for namespaces.

Also, modules provide as a mechanism for multiple inheritance via mix-ins and cannot be instantiated like classes can.

 

3. Explain this ruby idiom: a ||= b

a = b when a == false
Otherwise a remains unchanged.

4.What’s the issue with the controller code below? How would you fix it?

This is a classic example of the “n+1” bug. The first line will retrieve all of the Post objects from the database, but then the very next line will make an additional request for each Post to retrieve the corresponding Comment objects. To make matters worse, this code is then making even more database requests in order to retrieve the Author of each Comment.
This can all be avoided by changing the first line in the method to:

posts = Post.includes(comments: [:author]).all
This tells ActiveRecord to retrieve the corresponding Comment and Author records from the database immediately after the initial request for all Posts, thereby reducing the number of database requests to just three.

5. What paths (HTTP verb and URL) will be defined by the following snippet in config/routes.rb?

Using the resource method to define routes will automatically generate routes for the standard seven restful actions:
GET /posts
POST /posts
GET /posts/new
GET /posts/:id/edit
GET /posts/:id
PATCH/PUT /posts/:id
DELETE /posts/:id
GET /posts/:id/comments
POST /posts/bulk_upload

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

7. Explain a polymorphic association:

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

123
image by Rubyonrails.org

Untitled drawing (2)

image by Rubyonrails.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 polymorphic here is that it allows you to create a single comment model, rather than separate models for each one (PhotoComment model, ArticleComment model, etc.)

8. What Is the Difference Between a Block, a Proc, and a Lambda?

I just love Adam Waxman’s blog post about the subject, so with your permission i’ll refer you to it.

9. What is the Asset Pipeline?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, Sass and ERB. Read more here.

10. How can you achieve Multiple Inheritance in Ruby?

Ruby offers a very neat alternative concept called mixin. Modules can be imported inside other class using mixin. They are then mixed-in with the class in which they are imported.
Here’s an example:

As you can see above the class Debug and it’s method “whoamI?” were mixed-in (added) with the class Photo.

That’s why you can now create an instance of the Photo class and call the whoAmI? method.

11. Define the Rails MVC implementation using an example.

a) Model (ActiveRecord) – Maintains the relationship between Object and Database and handles validation, association, transactions, and more. This layer provides an interface and binding between the tables in a relational database and the Ruby program code that manipulates database records. Ruby method names are automatically generated from the field names of database tables, and so on.
b) Controller (ActionController) – The facility within the application that directs traffic, on the one hand querying the models for specific data, and on the other hand organizing that data (searching, sorting, massaging it) into a form that fits the needs of a given view.
c) View (ActionView) – A presentation of data in a particular format, triggered by a controller’s decision to present the data. Every Web connection to a Rails application results in the displaying of a view.

image by en.wikibooks.org
image by en.wikibooks.org

 

12. What is a Filter ? When it is called?

Filters are methods that are called either before/after a controller action is called.
Say a user requests a controller action such as userdashboard/index
In such a case a filter can be setup so that the UserDashboard/index page is only accessible to loggedin users by adding the following lines towards the beginning of the page:

In the code above the condition “confirm_logged_in” is checked before all actions, except login, logout & attempt_login.
After filters (after_filter) are not used too much but they have the effect of executing some code after a particular action has completed.
Think of them like triggers that get executed automatically – just like a database trigger.

13. Given:

x = “hello”

Explain the difference between:

and

The += operator re-initializes the variable with a new value, so a += b is equivalent to a = a + b.
Therefore, while it may seem that += is mutating the value, it’s actually creating a new object and pointing the the old variable to that new object.
This is perhaps easier to understand if written as follows:
foo = “foo” foo2 = foo foo.concat “bar” puts foo => “foobar” puts foo2 => “foobar” foo += “baz” puts foo => “foobarbaz” puts foo2 => “foobar”
(Examining the object_id of foo and foo2 will also demonstrate that new objects are being created.)
The difference has implications for performance and also has different mutation behavior than one might expect.

 

About The Author

reskill

Co-founder and CTO at ReSkill

  • Ben

    For 3, a will also be set equal to b if it’s nil or unassigned, right?

    • Wayne Chu

      Yep, that’s why sometimes we can use @a ||= A.find(id) to cache it.

    • Ілар Хоругин

      a ||= b really means a || a = b , because “a” IS SET ONLY when “a” is logical false (nil or false).
      Common misconception is to think that a ||= b is equal to a = a || b.

      • Evan Sparkman

        It’s also commonly referred to as Memoization.

  • Rafał Jamróz

    #12 : instead of before/after_filter we should now use before/after_action ( http://edgeguides.rubyonrails.org/4_2_release_notes.html#action-pack-notable-changes )

  • http://www.facebook.com/the.electro.dream Dreamr OKelly

    #3 – If A is NIL then B, otherwise A – it is called memoization

  • Mateusz

    Here: “That’s why you can now create an instance of the Photo class and call the whoAmI? method.”

    You probably meant “Phonograph” class.

    • ronsho

      You’re correct, the names were incorrect. Thanks!

  • http://www.desafioLatam.com Gonzalo Sanchez

    12) it’s better to use before_action, the intention of what it does is much more clear.

  • Jeff Summers

    I think these questions are good things to know of course, but in a job interview situation you probably won’t see questions of this format. Instead you’ll most likely be given a Ruby coding test to evaluate your skill level, prior to the interview in most cases.

Close