December Code

Ruby for placements: interview questions by topic

Ruby is best known through Ruby on Rails, which still runs large products and many startups' back ends, and companies hiring for Rails teams interview on the language as much as on the framework. Ruby looks friendly, but its friendliness rests on rules that differ from other languages: only nil and false are falsy, every value is an object, strings are mutable while symbols are not, and a block can change what return means.

Interview questions therefore test those rules. Expect to explain the difference between a proc and a lambda, what self is in a class body, why attribute methods exist, how include differs from extend, what each returns compared with map, and which exceptions a bare rescue catches. Many questions show a few lines of code and ask what they print, because predicting output is the quickest check that someone knows the rules rather than the vocabulary.

The pages below take one topic each, with answers whose code was run on Ruby 3.4, and one question exactly as the diagnostic asks it. The diagnostic draws from every Ruby topic in the bank and names the ones to read first.

New to this? How to prepare for placements — the method — and what the readiness test measures.

Ruby topics, one page each

8 of the 8 Ruby topics in the bank have a page so far; the diagnostic draws from all 8.

  1. Ruby basics interview questions

    Ruby basics for interviews: everything is an object, truthiness, nil and safe navigation, conversions, ranges, type checks and variable kinds.

  2. Ruby strings and symbols interview questions

    Ruby strings and symbols for interviews: symbols versus strings, formatting, freeze, bang methods, split and join, regex and UTF-8 lengths.

  3. Ruby array and hash interview questions

    Ruby arrays and hashes for interviews: creating and reading them, push and shift, fetch, iterating, merging, cleaning up arrays and splats.

  4. Ruby blocks, procs and lambdas interview questions

    Ruby blocks for interviews: what a block is, yield and block_given?, procs, creating and calling lambdas, closures, next and break, and currying.

  5. Ruby OOP interview questions

    Ruby OOP for interviews: classes and initialize, attr_accessor, self and class methods, protected, super, class-level state and open classes.

  6. Ruby modules and mixins interview questions

    Ruby modules for interviews: namespaces, include versus extend, modules versus classes, Comparable, ancestors, module_function and included.

  7. Ruby Enumerable interview questions

    Ruby Enumerable for interviews: each versus map, select, reject and find, reduce, group_by and sort_by, custom Enumerables and Enumerators.

  8. Ruby exception handling interview questions

    Ruby exceptions for interviews: begin, rescue, else and ensure, the StandardError hierarchy, custom errors, raise, cleanup, rescue modifiers, cause.

One question, exactly as the diagnostic asks it

From the Ruby bank — 30 questions across 8 topics. The answer is marked because this one is public; in a sitting you choose first, then see why each option is right or wrong.

Blocks, Procs & Lambdas · easyRB-012

What does this Ruby code print?

def twice
  yield
  yield
end

count = 0
twice { count += 1 }
puts count
  1. 11
  2. 22correct
  3. 30
  4. 4It raises LocalJumpError: no block given

A method can receive a block without declaring it, and yield calls that block; twice yields two times, so the block runs twice. Blocks are closures, so count += 1 inside the block updates the count variable from the surrounding code, which ends at 2. 1 assumes the block runs once. 0 assumes the block works on a copy of count. LocalJumpError, no block given (yield), is what twice would raise if it were called without a block; here one is given. block_given? lets a method check before yielding.

More on this topic: Ruby blocks, procs and lambdas interview questions

Measure it

Reading the answers tells you what’s true. A diagnostic tells you what you get wrong.

10 Ruby questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: 1 diagnostic a month and 15 problems a day. No card.

By Harshit · updated