Skip to content

Ruby Interview Questions

These questions recur across Ruby and Rails interviews. Most have a short correct answer and a widespread wrong one; the wrong ones are called out, because repeating them is worse than silence.

Ruby questions test whether you have used the language, not whether you have read about it. The themes are truthiness (only nil and false are falsy), the precedence of and versus &&, blocks versus procs versus lambdas, shallow copies, the ancestor chain, and the GVL. If you can predict the output of a snippet and say why, you pass.

They differ only in precedence. && binds tighter than assignment; and binds looser.

val1 = true and false
val1 # => true
val2 = true && false
val2 # => false

The first parses as (val1 = true) and false: the assignment happens, then false is discarded. The second parses as val2 = (true && false). Use &&/|| in expressions and and/or only for control flow. The same trap applies to not: not x == y is not(x == y), while !x == y is (!x) == y.

Both produce a shallow copy; clone also copies frozen state and the singleton class, dup does not.

original = "hello".freeze
original.dup.frozen? # => false
original.clone.frozen? # => true
obj = Object.new
def obj.special = :only_here
obj.dup.special # NoMethodError — singleton methods are not copied
obj.clone.special # => :only_here

include Greeting makes Greeting#hello an instance method of the class; extend Greeting makes it a class method (it is include on the singleton class); prepend Greeting inserts the module ahead of the class in the lookup chain, so it wins over a method of the same name. With a module defining hello, A.new.hello, B.hello and C.new.hello all return "hello" — the difference is who can call it and which definition wins.

proc { |a, b| }.call(1) returns [1, nil] — procs are lenient about arity — while ->(a, b) { }.call(1) raises ArgumentError. return in a lambda returns from the lambda; in a proc it returns from the method that defined it, raising LocalJumpError if that method already returned.

Two inverse things: &block in a parameter list captures the block as a Proc; &obj in an argument list converts a Proc (or anything with to_proc) into a block. A captured block is always a proc — lambda? is false. [1, 2].map(&:to_s) works because :to_s.to_proc is equivalent to ->(x) { x.to_s }.

A proc or lambda keeps its defining scope alive after the method returns — that is why def counter; c = 0; -> { c += 1 }; end works.

(1..10).each_with_object([0, 1]) { |_, a| a << a[-1] + a[-2] }
# => [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
fib = Hash.new { |h, n| h[n] = n < 2 ? n : h[n - 1] + h[n - 2] }
fib[50] # => 12586269025 — memoised, and the default block is lazy
module M1; def who = "M1"; end
module M2; def who = "M2"; end
class Parent; def who = "Parent"; end
class Child < Parent
include M1
include M2
end
Child.ancestors # => [Child, M2, M1, Parent, Object, Kernel, BasicObject]
Child.new.who # => "M2"

Order: singleton class → prepended modules → the class → included modules (last first) → superclasses → Object → Kernel → BasicObject. method_missing is consulted only after the chain fails.

Threads give concurrency: the GVL serialises Ruby bytecode but is released during I/O, so a threaded server handles many in-flight requests. Processes give parallelism: fork, Puma workers, Sidekiq processes. Ruby has no async/await syntax — it has Fibers, Fiber.scheduler and the async gem. See Concurrency in Ruby.

a == b compares values, a.eql?(b) value and type, a.equal?(b) identity: 1 == 1.0 is true but 1.eql?(1.0) is false. Hash compares keys with eql? and buckets with #hash.

  1. Why does Hash use eql? and hash rather than ==?
  2. What is the difference between super and super()?
  3. What does freeze guarantee, and how do you make an object deeply immutable?
  4. What happens if you define method_missing without respond_to_missing??
  • val1 = true and false gives true. Precedence, not a bug.
  • Claiming dup is a deep copy. It is shallow, like clone.
  • Claiming clone shares mutable state while dup copies it. Both share it.
  • Saying extend adds instance methods. It adds singleton methods.
  • Saying Ruby has async/await. It does not.
  • Using and/or inside a value expression. The value is not what you expect.