Mastering the Technical Interview: Must Know Ruby on Rails Questions
By: Madeline Stalter
Tags: Interview Questions, Ruby on Rails, Ruby, Interview, Back End Development

As a Full-Stack Engineer and after my last article on React, it only seems right to also inform my readers about 22 essential questions regarding Ruby on Rails that will help them ace the verbal portion of the interview.

1. What is Ruby on Rails?

Ruby is an objected oriented programming language that was created in 1995 by Yukihiro Matsumoto.

Rails is a back end framework that’s compatible with the Ruby library for building web applications.

2. How Does Ruby on Rails Use the MVC Framework?

MVC stands for Model View Controller and is an architectural pattern that favors convention over configuration.

Model : A Ruby class that handles the domain logic of an application. In the model, you can perform complex database queries, draw data relationships, create custom algorithms, assign data attributes, and more.

: A Ruby class that handles the domain logic of an application. In the model, you can perform complex database queries, draw data relationships, create custom algorithms, assign data attributes, and more. The model utilizes the Active Record Library / inherits from ActiveRecord::base in order to form a bridge between the Ruby program code and the relational database. This inheritance allows the model to access a number of methods that assists in working with the database, without having the write them out explicitly.

It’s important to remember to follow the single responsibility principle here. If any methods in the model perform tasks that are out of scope, they should be moved to their own class.

View : Renders any information the controller sends it from the database. This is implemented by using the Action View Library that is based on Embedded Ruby (ERB) and determines how data will be presented. (Note: if you’re using a front end framework, the views folder will be empty).

: Renders any information the controller sends it from the database. This is implemented by using the Action View Library that is based on Embedded Ruby (ERB) and determines how data will be presented. (Note: if you’re using a front end framework, the views folder will be empty). Controller: Manages data flow between the router, models, and views. Transmits data requests from the user to the model and then delivers data that is rendered in the view to the user. Utilizes 7 actions that allow the application to display the index page (index), display the show page (show), create a new instance (new, create), edit an instance (edit, update), and delete an instance (destroy).

3. How Does Rails Implement Asynchronous JavaScript and XML (AJAX) Requests?

AJAX is a suite of technologies used to retrieve data from the server without having to refresh the webpage making that request. The Rails method of implementing AJAX operations is simple:

1. A trigger (e.g., a user clicking on a button to view a list of adoptable dogs) is fired.

2. The web client uses JavaScript to send data via an XMLHttpRequest from the trigger to an action handler on the server.

3. On the server-side, a Rails controller action receives the data and returns the corresponding HTML fragment to the client.

4. The web client receives the fragment and updates the view accordingly.

4. What is a Rails Migration?

A Rails Migration is a process that can be used to rename a table, add a column, change a column, drop a table, remove a column and more. Example commands include:

Rails g migration create_users name age:integer email_address

Rails g migration add_phone_number_to_users phone_number:string

5. What are Routes?

Routing in Rails allows for the application to know what view to render to the user. Rails routing connects incoming HTTP requests to the code in your application’s controllers and helps generate URLs without having to hard-code them as strings.

When a URL is entered into a browser, an HTTP request is made. That request is sent to the server where the application’s router (i.e., routes.rb file) interprets the request and sends a message to the controller mapped to that route.

The controller communicates with the view file mapped to the controller action.

The server returns that HTTP response, which contains the view page that can be viewed in the browser.

There are 7 standard RESTful routes. Defining routes with the “resources” method allows you to automatically generate all 7 routes.

6. What are the HTTP Verbs?

The GET method retrieves whatever information is identified by the Request URI.

method retrieves whatever information is identified by the Request URI. The POST method is used to send data enclosed in the request to the server. The server is expected to use this data to create some new resource.

method is used to send data enclosed in the request to the server. The server is expected to use this data to create some new resource. The PATCH/PUT methods both represent the HTTP verbs that are used to update existing resources.

methods both represent the HTTP verbs that are used to update existing resources. The DELETE method requests that the server delete the resource identified by the Request URI.

7. Why is *Almost* Everything in Ruby an Object?

Objects are considered to be bundles of data that we can perform methods on. In object-oriented programming, an object is an instance of a class. In Ruby, classes are also objects (all classes are instances of the class Class).

Only a handful of things are not objects such as blocks, methods, and conditional statements.

7. Is Ruby Statically or Dynamically Typed?

Ruby is dynamically typed meaning you can change the type and/or value of a variable without error.

Example:

x = 16

x = "hello world"

8. What’s the Difference Between Getter and Setter Methods?

Getter methods allow us to access instance variables, while setter methods allow us to set instance variables.

Ruby provides three accessor methods that allow us to get and set instance variables: attr_reader (getter), attr_writer (setter), and attr_accessor (setter and getter). Example:

class Dog

attr_accessor :color

end dog1 = Dog.new

dog1.color = "black"

puts dog1.color #=> black

9. How Do You Call a Method In Ruby and What Happens When You Do?

You can call a method in Ruby using dot notation (or the dot operator).

For example, using our dog1 instance from above — we called dog1.color in order to return the color of that specific dog instance.

9. What is a Gemfile?

A Gemfile is where dependencies are specified; it is located in the project’s root directory.

10. What is a Gemfile.lock?

A Gemfile.lock contains records of exact versions of gems installed. This is done so that the same versions can be installed if another machine clones the project.

If you delete the Gemfile.lock or do not specify a specific version of gem, it will just install the latest version when cloning the project to a new machine.

11. What is the Difference Between Count, Length, and Size?

Count: executes a SQL query to count the number of records. This is useful if the number of records may have changed in the database vs. memory.

Length: returns the number of items in a collection in memory. Its lighter weight than count because no database transaction is performed. It can also be used to count characters in a string.

Size: is an alias for length and does the same thing.

12. What are Callbacks?

Callbacks are hooks in the object lifecycle where you can execute methods. A variety of callbacks exist around creating, updating, and destroying an object such as before_validation, after_save, and after_destroy.

Callbacks are useful for conditional logic; for example, when creating an associated record when a user record is created.

13. What is the Difference Between Class and Instance Methods?

As their names suggest, class methods are available on classes and instance methods are available on instances.

Class methods are denoted by:

def self.method_name

end

*Self refers to the current class.

For example, for a class Car , an instance method may check the average speed of a specific car, whereas the class method may assess the average number of miles driven across all cars.

14. Does Ruby Allow Multiple Inheritances?

Ruby does not allow inheriting from more than one parent class, but it does allow mixing modules with include and extend.

15. Is Ruby Strongly or Weakly Typed?

Ruby is strongly typed; you cannot concatenate data types.

For example, “hello” + 3 would throw an error in Ruby.

16. How do we Declare a Constructor Class in Ruby?

A constructor is defined with the initialize method that is called when a new instance of a class is created. For example:



attr_reader :breed

def initialize(breed)



end

end class Puppyattr_reader :breeddef initialize(breed) @b reed = breedendend Sidney = Puppy.new("German Shepherd")

puts Sidney.breed # => German Shepherd

17. What is Rack?

Rack is an API sitting between the web server and Rails; it allows us to plug in and swap frameworks like Rails with Sinatra or web severs like Unicorn with Puma.

18. What is Yield?

Yield accesses a code block passed into a method. For example:

def yield_example

puts "first line"

yield if block_given?

puts "third line"

yield if block_given?

end yield_example { puts "yielding" }

# => first line

# => yielding

# => third line

# => yielding

18. What’s the Difference Between a Hash and JSON?

A Hash is a collection of key/value pairs.

JSON is a string in a specified format for sending data.

19. What are Some Advantages of Rails?

User friendly; gentle learning curve given its prioritization of convention over configuration.

Widely backed by the open source community; lots of resources and support.

19. What are Some Disadvantages of Rails?

Run and boot time can be slow.

20. What is Spring?

Spring preloads applications; keeping the application running int he background so booting is not required when you run a migration or rake task.

21. What is the Difference Between Select, Map, and Collect?

Select: Used to grab a subset of a collection. Example:

i = [1,2,3,4,5]

i.select {|x| x % 2 == 0}

# => [2, 4]

Map: Performs an action on each element of a collection and outputs an updated collection. Example:

i = [1,2,3,4,5]

i.map {|x| x+1}

# => [2,3,4,5,6]

Collect: Is an alias of Map and does the same thing.

22. What are the Three Levels of Access Control?