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.
The 60-second answer
Section titled “The 60-second answer”Ruby questions test whether you have used the language, not whether you have read about it. The themes are truthiness (only
nilandfalseare falsy), the precedence ofandversus&&, 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.
1. and vs &&
Section titled “1. and vs &&”They differ only in precedence. && binds tighter than assignment; and binds looser.
val1 = true and falseval1 # => true
val2 = true && falseval2 # => falseThe 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.
2. dup vs clone
Section titled “2. dup vs clone”Both produce a shallow copy; clone also copies frozen state and the singleton class, dup does not.
original = "hello".freezeoriginal.dup.frozen? # => falseoriginal.clone.frozen? # => true
obj = Object.newdef obj.special = :only_hereobj.dup.special # NoMethodError — singleton methods are not copiedobj.clone.special # => :only_here3. include vs extend vs prepend
Section titled “3. include vs extend vs prepend”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.
4. Blocks, procs and lambdas
Section titled “4. Blocks, procs and lambdas”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.
5. What does & do?
Section titled “5. What does & do?”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 }.
6. Closures and the Fibonacci one-liner
Section titled “6. Closures and the Fibonacci one-liner”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 lazy7. Method lookup
Section titled “7. Method lookup”module M1; def who = "M1"; endmodule M2; def who = "M2"; endclass Parent; def who = "Parent"; end
class Child < Parent include M1 include M2end
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.
8. Concurrency vs parallelism in Ruby
Section titled “8. Concurrency vs parallelism in Ruby”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.
9. ==, eql? and equal?
Section titled “9. ==, eql? and equal?”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.
Follow-up questions
Section titled “Follow-up questions”- Why does
Hashuseeql?andhashrather than==? - What is the difference between
superandsuper()? - What does
freezeguarantee, and how do you make an object deeply immutable? - What happens if you define
method_missingwithoutrespond_to_missing??
Common traps
Section titled “Common traps”val1 = true and falsegivestrue. Precedence, not a bug.- Claiming
dupis a deep copy. It is shallow, likeclone. - Claiming
cloneshares mutable state whiledupcopies it. Both share it. - Saying
extendadds instance methods. It adds singleton methods. - Saying Ruby has
async/await. It does not. - Using
and/orinside a value expression. The value is not what you expect.