All interview guides

Ruby Interview Questions and Answers

24 questions that come up in Ruby technical interviews, each with the answer and an explanation of why it is right.

Test yourself — 90 question bank

1. What is a module?

Intermediate

Answer: Container for methods and constants; used for mixins

Modules group methods/constants. Use for namespacing and mixins. module MyModule; end. Include for instance methods, extend for class methods. Cannot instantiate.

2. What is singleton class/eigenclass?

Advanced

Answer: Hidden class unique to each object

Singleton class is hidden class for each object. Access with class << obj. Define singleton methods here. Understanding crucial for metaprogramming. Part of Ruby object model.

3. What is Ruby?

Beginner

Answer: Dynamic, object-oriented programming language

Ruby is a dynamic, interpreted, object-oriented programming language created by Yukihiro Matsumoto (Matz). Focuses on simplicity and productivity with elegant syntax.

4. What is the principle behind Ruby?

Beginner

Answer: Principle of Least Surprise (POLA)

Ruby follows POLA (Principle of Least Surprise) - the language should behave in a way that minimizes confusion. Designed to be intuitive and natural.

5. What is the difference between include and extend?

Intermediate

Answer: include adds instance methods, extend adds class methods

include mixes module methods as instance methods. extend mixes as class methods. prepend inserts before (override). Use for different composition needs.

6. What is class << self?

Advanced

Answer: Opens singleton class for defining class methods

class << self opens singleton class. Define class methods here. Alternative to self.method. More common in gems/frameworks. Cleaner for multiple class methods.

7. What is const_missing?

Advanced

Answer: Called when constant not found

const_missing called when constant undefined. Implement autoloading, lazy loading. Rails uses for autoloading. Powerful but can hide errors. Define on module/class.

8. What is a lambda?

Intermediate

Answer: Anonymous function with -> or lambda

Lambda: square = ->(x) { x * 2 } or lambda { |x| x * 2 }. Call with .call(). Strict argument checking. return exits lambda only. Closure captures variables.

9. How do you output text in Ruby?

Beginner

Answer: puts or print

Use puts (adds newline) or print (no newline). p for debugging (shows inspect output). puts most common for output. Example: puts "Hello World".

10. What is method lookup chain?

Advanced

Answer: Order Ruby searches for methods

Method lookup: object → singleton class → class → included modules (reverse order) → superclass → ... → BasicObject. Use .ancestors to see chain. Important for understanding behavior.

11. How do you declare a variable?

Beginner

Answer: name = value (no declaration keyword)

Variables declared by assignment: name = "John". No var/let keyword. Lowercase/underscore for local variables. Dynamic typing - type determined at runtime.

12. What is a Proc?

Intermediate

Answer: Object that holds block of code

Proc is object wrapping block: proc = Proc.new { |x| x * 2 }. Call with .call(). Lenient arguments. return exits enclosing method. Convert block to object.

13. What are the variable naming conventions?

Beginner

Answer: local_var, @instance_var, @@class_var, $global, CONSTANT

local_variable (snake_case), @instance_variable, @@class_variable, $global_variable, CONSTANT (uppercase). Conventions indicate scope. Very important in Ruby.

14. What is prepend?

Advanced

Answer: Includes module before class in lookup chain

prepend inserts module before class in lookup. Methods override class methods. Use for wrapper/decorator pattern. Ruby 2.0+ feature. More powerful than include.

15. What is the difference between lambda and Proc?

Intermediate

Answer: Lambda strict args and return; Proc lenient

Lambda: strict argument checking, return exits lambda. Proc: lenient arguments, return exits enclosing method. Lambda more like method. Choose based on needs.

16. What is a symbol?

Beginner

Answer: Immutable identifier starting with :

Symbol is immutable identifier: :name, :status. More memory efficient than strings. Same symbol has same object_id. Use for hash keys, identifiers.

17. What is yield?

Intermediate

Answer: Calls block passed to method

yield executes block passed to method. def greet; yield "Hello"; end. Call: greet { |msg| puts msg }. block_given? checks if block passed. Implicit block passing.

18. What is refinements?

Advanced

Answer: Scoped monkey patching

Refinements scope monkey patches: refine String { def shout; upcase; end }. Use with using. Lexically scoped modifications. Safer than global monkey patching.

19. What is Fiber?

Advanced

Answer: Lightweight concurrency primitive

Fiber provides cooperative concurrency. Create with Fiber.new. Control with resume/yield. More lightweight than threads. Use for generators, async programming.

20. What is the difference between string and symbol?

Beginner

Answer: Symbols immutable, unique; strings mutable, multiple instances

Symbols are immutable, one instance per value. Strings are mutable, new instance each time. Use symbols for identifiers, strings for text data.

21. What is block_given??

Intermediate

Answer: Checks if block passed to method

block_given? returns true if block passed. Use to make blocks optional: yield if block_given?. Avoids errors when no block. Good defensive programming.

22. What is Thread?

Advanced

Answer: True concurrency with OS threads

Thread provides real OS threads. Thread.new { code }. Use Mutex for synchronization. GIL limits parallelism in MRI. Better for I/O than CPU-bound. Thread-safe coding needed.

23. How do you define a method?

Beginner

Answer: def method_name; end

Define methods with def: def greet(name); "Hello #{name}"; end. Implicit return (last expression returned). Convention: snake_case names, ? for predicates, ! for mutating.

24. What is the & operator for blocks?

Intermediate

Answer: Converts block to Proc or Proc to block

& converts: block to Proc in parameters (def method(&block)), Proc to block in calls (method(&proc)). Explicit block handling. Enables passing blocks as objects.

Ready to test yourself?

The full Ruby bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.

Take the Ruby quiz