Advanced Ruby test (Advanced)
40 questions · 40 minutes · Free
Metaprogramming, Rails at scale and performance.
What it covers
- Metaprogramming
- ActiveRecord performance and N+1
- Background jobs
- Concurrency and the GVL
- Rails security
Interview format
- Questions: 40 questions — 24 multiple choice, 12 multi-answer, 4 predict-the-output
- Time: 40 minutes, timed from the first question
- Difficulty: Advanced
- Skill area: Backend
- Price: Free
Sample questions
5 sample questions from the Advanced Ruby test, with the answers. Samples are taken out of the live test, so you will not meet these when you sit it.
Which of the following correctly describes the behavior of `prepend` when adding a module to a class?
- prepend inserts the module before the class in the ancestors chain
- extend inserts the module before the class in the ancestors chain
- prepend inserts the module after the class in the ancestors chain
- include inserts the module before the class in the ancestors chain
Answer: A. prepend inserts the module before the class in the ancestors chain
In Ruby, `prepend` adds a module to the ancestors chain before the class itself, allowing it to override methods with `super` for wrapping behavior. `include` adds the module after the class, and `extend` adds methods to an object's singleton class.
Which of the following best describes the correct usage of define_method in Ruby?
- define_method is used to override existing methods in a module.
- define_method can be used independently without any evaluation context.
- define_method is used to dynamically create classes at runtime.
- define_method is used to dynamically define methods in a class, and it is typically called within class_eval or module_eval blocks.
Answer: D. define_method is used to dynamically define methods in a class, and it is typically called within class_eval or module_eval blocks.
define_method is used to create methods dynamically, but it must be called within the context of a class or module using class_eval or module_eval. Option 3 correctly states this, while the other options either misrepresent the purpose or the context in which define_method is used.
Which statement correctly describes how Rails prevents XSS attacks in views by default?
- All user-provided content is automatically rendered as HTML in ERB templates.
- Using `html_safe` is the recommended way to prevent XSS in views.
- ERB escapes output by default, preventing XSS attacks.
- XSS is prevented by default in Rails without any escaping mechanism.
Answer: C. ERB escapes output by default, preventing XSS attacks.
In Ruby on Rails, ERB templates automatically escape any output to prevent cross-site scripting (XSS) attacks. This ensures that any user-provided content is rendered as text rather than executable HTML or JavaScript. Using methods like `raw` or `html_safe` bypasses this escaping, which should be done carefully to avoid security vulnerabilities.
You have a `User` model with a `has_many :posts, through: :user_posts` association and a `has_many :comments, through: :user_comments` association. When querying `User.all`, you notice N+1 queries are being generated for both `posts` and `comments`. What is the correct approach to avoid this?
- Use `preload(:posts, :comments)` to load both associations with separate queries.
- Use `includes(:posts, :comments)` to eager load both associations in a single query.
- Use `eager_load(:posts, :comments)` to load both associations with a single LEFT OUTER JOIN.
- Use `joins(:posts, :comments)` to avoid generating any additional queries.
Answer: B. Use `includes(:posts, :comments)` to eager load both associations in a single query.
When using `has_many :through`, Rails may generate N+1 queries if associations are not eager loaded properly. `includes` with multiple associations ensures that both are eager loaded in a single query, avoiding N+1. `preload` and `eager_load` are not as effective in this scenario because they do not handle the intermediate join table correctly. `joins` does not load the associated objects, which is not helpful here.
What is the primary purpose of strong parameters in Rails?
- Strong parameters are used to encrypt sensitive data in the application.
- Strong parameters are used to sanitize user input before saving it to the database.
- Strong parameters prevent mass-assignment attacks by explicitly permitting attributes.
- Strong parameters are used to filter out malicious SQL queries.
Answer: C. Strong parameters prevent mass-assignment attacks by explicitly permitting attributes.
Strong parameters are a security feature in Rails that prevents mass-assignment vulnerabilities by allowing developers to explicitly specify which attributes can be assigned during an update or create action. This ensures that only the permitted attributes are processed, reducing the risk of unauthorized data modification.
Frequently asked questions
- How many questions are in the Advanced Ruby test?
- 40 questions (24 multiple choice, 12 multi-answer, 4 predict-the-output), with 40 minutes to answer them.
- What does the Advanced Ruby test cover?
- Metaprogramming, ActiveRecord performance and N+1, Background jobs, Concurrency and the GVL, Rails security.
- Is the Advanced Ruby test free?
- Yes. It is free to take with a readyforhires account.
- What level is the Advanced Ruby test?
- Advanced, in Backend.