पाठ 25 / 25

Revision and Interview Questions

Recall Ruby concepts quickly for exams and interviews.

Cheat sheet

Basics: everything is an object; only nil and false are falsy; strings vs symbols; # frozen_string_literal: true; Integer, Float, BigDecimal for money; Integer() vs to_i. Tools: version managers, irb, gems, Bundler, Gemfile and Gemfile.lock, bundle exec, ~> constraints. Control: expressions return values, unless, modifiers, case/when with === (ranges, classes, regexes), ranges including endless ones. Collections: Array, Hash (symbol keys, value omission), Enumerable (map, select, reduce, group_by, tally, partition, each_with_object, filter_map), bang methods, &:sym, lazy enumerators. Blocks: yield, block_given?, &block, _1 and it, procs vs lambdas, closures. OOP: initialize, attr_reader, class methods with self., ?/! conventions, endless methods, modules as namespaces and mixins, include/extend/prepend, Enumerable and Comparable, ancestors, single inheritance with super, public/private/protected, Struct and Data. Methods: positional, defaults, splats, keyword and required keyword arguments, **opts, (...) forwarding. Errors: raise, rescue, else, ensure, retry, custom errors under StandardError. Pattern matching: case/in, =>, in, guards, pin, deconstruct_keys. Meta: duck typing, open classes, refinements, send, define_method, method_missing with respond_to_missing?, DSLs with instance_eval. Practical: File, JSON, CSV, Time, regex anchors \A/\z, GVL, threads for I/O, Ractors, fibers. Ecosystem: gems and SemVer, RSpec and Minitest, RuboCop, RBS/Sorbet, YJIT, Rails MVC.

Common interview questions

Answer each with a short code example.

1. What does it mean that everything in Ruby is an object?
2. Which values are falsy in Ruby?
3. What is the difference between a symbol and a string?
4. Explain blocks, procs and lambdas and how they differ.
5. include vs extend vs prepend?
6. How does method lookup work (ancestors)?
7. What are bang (!) and predicate (?) methods?
8. How does case/when use ===? How does case/in differ?
9. What is the GVL, and when do threads help?
10. What are method_missing and respond_to_missing? for?
11. What problems can monkey patching cause, and what are refinements?
12. Explain Gemfile vs Gemfile.lock and what bundle exec does.

Show idiomatic code

Interviewers for Ruby roles look for idiomatic style. Rewriting a loop as a short Enumerable chain, and explaining why it is clearer, is a quick way to demonstrate fluency.

त्वरित जाँच: Which method do you define to make your own class work with Enumerable?

  • to_s
  • initialize
  • inspect
  • each
Answer

each — Enumerable builds its methods on each.