53 questions and interviews with Ruby on Rails

Tram Ho

(Questions you might be asked in Ruby on Rails interview as Junior / Middle Developer)

As usual, in the middle of the month, I hang around on Medium to do what everyone knows what to do. I found this article to be quite useful, so I decided to translate to save the knowledge myself, and also share it with everyone. Maybe one fine day you want to dance, or a cloudy day, you will interview your company’s candidates. Anyway, “knowledge is endless, knowing a little more is good, so absorbed in my body, I won’t go anywhere but fear”, that’s my mother always said so, so I boldly translated, hoping someone did. Read here will boldly read more !!

Above is the opening of my post, and this is the author’s article, I will summarize: The author has been interviewed or been interviewed a lot for the position of Ruby on Rails Developer and hope for my experience. I will be helpful for anyone who is about to do an interview. Below are the questions that the author received or gave to his / her candidate, the questions will not be in any order.

1. How Rails works after having a request to access the list of posts in the blogging application?

When the user clicks on the button that creates a GET request to the URL /articles , the web server receives this request. Rails will then execute the corresponding controller action index based on the URL / controlller mapped from the routes.rb file. The Controller calls Article.all to get the articles in the database via the Artical model. This post list will be assigned to an instance variable and this variable is called on the form to display the list of posts to the user.

2. Why is it “Almost everything in Ruby is an object?”

In OOP, an object is an instance of a class. In Ruby, all classes are instances of Class Class. For example:

Some non-object things like block , method (methods) and conditional statements ( if , else … conditional else ). This question is raised to see if you understand that almost everything in Ruby works similarly, which makes Ruby more receptive than other languages.

3. Is Ruby a programming language static or dynamic?

Ruby is a dynamic language. This is the reason why you can change the type of the variable when executing code. In Ruby, the lines of code below run one line at a time without causing an error.

If you do not understand what static and dynamic programming languages ​​are, follow Wikipedia :

  • Static type language is a language that predetermines all data declared in the source code at the time of translation. Variable values ​​can only be of a certain type / number and we can only perform certain operations on them.
  • Dynamic languages ​​are those in which the types are assigned to data only during the time the program is executed. This has the advantage that the programmer does not need to specify a data type at all, and has the added advantage of being able to assign more than one data type to variables.

4. What do you know about getter and setter in Ruby?

A getter allows access to an instance variable. A setter allows to set an instance variable. We can define the setter and getter methods by ourselves as shown below:

However, Ruby provides three accessor methods to perform get and set one neatly: attr_reader (getter), attr_writer (setter) and attr_accessor (setter and getter).

5. What happens when you call a method in Ruby?

When a method is called, Ruby will do two things in turn: finding and executing. First it will find the method by going to the class of the object , then move from that class to the parent class it inherits, then from that parent class it continues to move to the next parent class. , until we meet the last ancestor class Object. On that way, if it finds the method being called, it stops searching and executes that method. If the last parent class is still not found, method_missing returned and the search ends.

6. How do I retrieve a list of the routes of a Rails application?

Open terminal and run the command:

We can also add | grep <keyword> after the above command to filter the routes containing the keyword .

7. What is Gemfile?

Gemfile is a file located in the root directory of the project, where the dependencies (in my understanding are gems – libraries) are specified in a Ruby application.

8. What is Gemfile.lock?

Gemfile.lock is a file located in the root directory of the project, it contains the exact version of the installed gems. If another machine clone the project, it would have the same gem versions installed. In contrast, Gemfile does not need to specify a specific version of the gem and will install the latest version for that gem.

9. Name some of the patern design (design patterns) Rails you used

There are several design patern in Rails including service objects, value objects, form objects, query objects, view objects, policy objects and decorators. You can dive into analyzing those patern designs with the examples here .

10. How does Rails manage database state?

After the migrations are generated and added intructions , these intructions instruct ActiveRecord how to modify the existing database state.

11. What is the difference between count , length and size ?

  • count : Executes an SQL query to count the number of records. This method is useful if the number of records may have changed in DB relative to memory.
  • length : Returns the number of items in the collection of memory. It is faster than count because no database transaction are performed. It can also be used to count characters in a string.
  • size : This is an alias of length and is used similarly.

12. How did you implement the decentralized functionality?

Authorization (not to be confused with authentication) involves allowing different types of users to have different access rights within an application. Some gems like Pundit and CanCanCan can be used to implement this function.

13. What is Callback?

Callbacks are a method of Active Record that will be called at some point in the life cycle of an object. Callbacks are often used to execute logical methods before or after the object has made a change, such as create, update, delete, … They are often used in parallel with data validation to make sure that the database data entry and exit is completely correct. However, if using Callback incorrectly, there will be some bad cases that affect testing and debugging.

Callbacks are used directly in conjunction with ActiveRecord methods such as create, save, update, and destroy records in the database.

15. What are intializers in Rails?

Intializers contain configuration logic and run only when the application is started. This means that the Rails server (Rails server) needs to be restarted if Intializers are changed. They exist in the /config /intializers .

16. What is the difference between delete and destroy?

  • delete : Delete a record.
  • destroy : Delete a record and execute callbacks.

Callbacks are often called destroy when specifying a relationship in model files. Example: Delete relevant comments when an article is deleted.

17. What do “Fat models, skinny controllers” mean when saying?

Business logic should be placed in models , not controllers . This makes it easier to unit test and reuse logic more efficiently.

Controllers are merely passing information between models and views .

This is often offered as advice for new Rails Devs. It is not really recommended, especially in large applications.

18. What do “skinny controllers, skinny models” mean?

As a codebase grows, models become swollen by handling too many things and become unmanageable. The models shouldn’t be too cumbersome with logic.

models can be simplified by preserving the basic elements and moving the logic out of the models, into other designs such as service objects .

19. What is the difference between class methods and instance methods?

class method are available on classes and instance method are available in the instance (of course). They are often used for different purposes.

For a class, Article , an instance method can count the number of words in the body of a particular Article . Whereas a class method can count the number of posts by a particular author on all article (note scope differences).

The class method are denoted by def self.method_name .

20. What is PORO?

PORO stands for “Plain Old Ruby Object”.

Even though almost everything in Ruby is an object, ActiveRecord also tends to use a lot of complex objects. Therefore, the term PORO is often used to emphasize a small, simple object that is used to support business logic.

21. Does Ruby allow multiple inheritance?

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

22. Is Ruby a strong or weak typed language?

Ruby is a strong typed language. An error will occur if you try to execute hello hello + 3 .

Looking at JavaScript, we will see that JavaScript is a weakly typed language, with the above example it will output all the results of “hello3” .

The post is a bit long so I will separate the rest for the next post!

Reference: Here

Share the news now

Source : Viblo