Ruby Logo

Style & Conventions Quiz

Check your understanding of Ruby style rules, idiomatic code patterns, and documentation guidelines.

Home Ruby Style & Conventions Quiz

Style & Conventions Quiz

Validate your understanding of the Ruby style guide, idiomatic patterns, RuboCop, and documentation expectations used across RubyDev. Each question includes an answer and explanation—treat it like a lightweight code review exercise.

Instructions: Review each scenario, pick the most accurate statement, then compare with the answer block. Aim to reason about *why* an option is correct—style decisions should feel deliberate.

1. Guard Clauses

Which snippet best follows the Ruby style guide for guarding invalid state?

A) if cart.nil? raise ArgumentError, 'missing cart' end process(cart) B) raise ArgumentError, 'missing cart' if cart.nil? process(cart) C) process(cart) unless cart.nil? D) process(cart) if cart.present?

Answer: B — the guard clause raises early, keeps the happy path left-aligned, and uses a descriptive exception.

2. Naming Conventions

Which pairing follows Rails loading conventions without additional configuration?

A) app/services/paymentService.rb → PaymentService B) app/services/payment/service.rb → PaymentService C) app/services/payment_service.rb → PaymentService D) app/services/payment_service.rb → Payment::Service

Answer: C — snake_case file names match CamelCase constants; nested directories are only required for namespaced classes.

3. RuboCop Usage

You need to ignore a cop for a single method because an upstream library requires it. What is the recommended approach?

A) Disable the cop globally in `.rubocop.yml`. B) Generate `.rubocop_todo.yml` and add the cop. C) Inline disable + enable around the method with a justification comment. D) Delete the cop from the RuboCop gem.

Answer: C — scope the disable to the minimal block and explain why, keeping the global configuration trustworthy.

4. YARD Docs

Which annotation set fully documents a public method that raises an error?

A) `# @param user` B) `# @param [User] user` C) `# @param user [User]` `# @return [Boolean]` `# @raise [AuthError]` D) `# user is a User`

Answer: C — include type information, return value, and documented exceptions for accurate generated docs.

5. Naming Edge Cases

You introduce a background job that checks expired trials. Which combination best matches conventions?

A) Class `trial_checker` in `app/jobs/trial_checker.rb` B) Class `TrialCheckerJob` in `app/jobs/trial_checker_job.rb` C) Class `TrialsChecker` in `app/jobs/trials_checker.rb` D) Class `trial_checker_job` in `app/jobs/trial_checker_job.rb`

Answer: B — jobs end with `Job`, live in `app/jobs`, and the file name mirrors the class with snake_case.

Level Up

  • Pair this quiz with `bundle exec rubocop --auto-gen-config` to see which cops your module violates.
  • Annotate a new service with YARD and regenerate docs to ensure zero undocumented public methods.
  • Refine `AGENTS.md` whenever the team agrees on a new exception to the style guide.

Quick Navigation

Related Topics

Video Tutorial

Watch and learn style & conventions quiz

Pro Tip: After reading through the content above, watch this video to reinforce your understanding and see the concepts in action!