Prepare for Your Ruby Interview: Basic to Advanced Questions
This comprehensive guide features 30 essential Ruby interview questions covering conceptual, practical, and scenario-based topics. Questions progress from basic to intermediate and advanced levels, perfect for freshers, 1-3 years, and 3-6 years experienced candidates preparing for roles at companies like Zoho, Atlassian, or Paytm.
Basic Ruby Interview Questions
1. What is Ruby and what makes it object-oriented?
Ruby is a dynamic, interpreted programming language designed for simplicity and productivity. Everything in Ruby is an object, including primitives like integers and strings, which makes it truly object-oriented. Classes and modules are also objects that can be manipulated at runtime.[6]
2. What are the main features of Ruby?
Ruby features include being object-oriented, flexible, dynamic typing with duck typing, garbage collection, and keyword arguments. It follows the Principle of Least Astonishment (POLA) to make behavior intuitive.[2][3]
3. How do you write comments in Ruby?
Ruby supports single-line comments with # and multi-line comments with =begin and =end.
=begin
This is a multi-line comment
in Ruby
=end
puts "Hello World" # Single line comment
4. What are Ruby variables and their naming conventions?
Ruby has local variables (starting with lowercase), instance variables (@prefix), class variables (@@prefix), and constants (UPPERCASE). Local variables are scoped to methods or blocks.[6]
5. What is the difference between symbols and strings in Ruby?
Symbols are lightweight, immutable identifiers (e.g., :name) used as hash keys. Strings are mutable sequences of characters. Symbols have a single memory location per symbol value.[1]
6. How do you define and call a method in Ruby?
Methods are defined with def and called by name. They can take parameters and return values.
def greet(name)
"Hello, #{name}!"
end
puts greet("Alice") # Output: Hello, Alice!
7. What are Ruby arrays and how do you manipulate them?
Arrays are ordered collections. Common methods include push, pop, map, and select.
numbers = [1, 2, 3, 4]
doubled = numbers.map { |n| n * 2 }
puts doubled.inspect # [2, 4, 6, 8]
8. Explain hashes in Ruby with an example.
Hashes are unordered key-value pairs. Keys can be symbols or strings.
user = { name: "Bob", age: 30 }
puts user[:name] # Bob
9. What are control structures in Ruby?
Ruby has if, else, elsif, unless, case, and loops like while, for, and iterators.[1]
10. How do you check if a method takes a block?
Use block_given? inside the method.
def with_block
if block_given?
yield
else
puts "No block provided"
end
end
with_block { puts "Block executed" }
Intermediate Ruby Interview Questions
11. What are iterators in Ruby? Name a few.
Iterators like each, times, upto, each_line provide a functional way to traverse collections.[3]
5.times { |i| puts i } # 0 1 2 3 4
12. Explain blocks, procs, and lambdas in Ruby.
Blocks are chunks of code passed to methods. Procs and lambdas are objects wrapping blocks. Lambdas check argument count strictly; procs are more flexible.[2]
13. What is the difference between super and super()?
super passes all arguments to the parent method. super() calls the parent method without arguments.[7]
14. How do you handle exceptions in Ruby?
Use begin, rescue, else, and ensure.
begin
risky_operation
rescue => e
puts "Error: #{e.message}"
ensure
puts "Cleanup"
end
15. What are access modifiers in Ruby?
Public (default), private (instance only), and protected (same class or subclasses).[4]
class Example
def public_method; end
private
def private_method; end
end
16. Write a method to filter even-length strings from an array.
[2]
def even_length_strings(array)
array.select { |str| str.length.even? }
end
puts even_length_strings(["hi", "hello", "hey"]).inspect
# ["hi", "hey"]
17. What is duck typing in Ruby?
Duck typing means “if it walks like a duck and quacks like a duck, it’s a duck.” Objects are treated based on capabilities, not class.[3]
18. How do you create and use modules in Ruby?
Modules provide namespaces and mixins (include/extend).
module Logger
def log(msg); puts msg; end
end
class App
include Logger
end
19. Explain method missing in Ruby.
method_missing is called when an undefined method is invoked, enabling dynamic method creation.[2]
20. What is attr_accessor and how does it work?
attr_accessor creates getter and setter methods for instance variables.
class Person
attr_accessor :name
end
p = Person.new
p.name = "Eve"
puts p.name # Eve
Advanced Ruby Interview Questions
21. What is metaprogramming in Ruby?
Metaprogramming lets code write or modify other code at runtime using define_method, eigenclass, etc. Common in DSLs.[2]
22. Explain the difference between class and module.
Classes can be instantiated and subclassed. Modules cannot be instantiated but can be mixed in.[3]
23. How does Ruby’s garbage collector work?
Ruby uses mark-and-sweep garbage collection to automatically manage memory by reclaiming unused objects.[3]
24. What are fibers in Ruby?
Fibers are lightweight, cooperatively-scheduled units of execution for concurrency without OS threads.
fiber = Fiber.new do
Fiber.yield "Hello"
"World"
end
puts fiber.resume # Hello
puts fiber.resume # World
25. Scenario: At Salesforce, you need to process user data safely. How do you implement transactions?
Use ActiveRecord::Base.transaction for atomic operations (concept applicable in pure Ruby patterns).[4]
26. Write a method that executes a block only if provided.
[2]
def execute_if_block(&block)
if block_given?
block.call
end
end
execute_if_block { puts "Block called!" }
27. What are singleton methods and classes?
Singleton methods belong to one object. Singleton classes store these methods.
obj = Object.new
def obj.unique_method; "Unique"; end
28. Scenario: Swiggy needs efficient caching. How would you implement a simple LRU cache in Ruby?
Use a hash with ordered keys for least-recently-used eviction.
class LRUCache
def initialize(size)
@cache = {}
@size = size
end
# Implementation details...
end
29. Explain Ruby’s send method and its uses.
send dynamically calls methods by name, useful for metaprogramming and avoiding protected method restrictions.
"hello".send(:reverse) # "olleh"
30. What are refinements in Ruby and when to use them?
Refinements scope monkey patches to specific classes/modules, preventing global pollution in large codebases like those at Oracle.
module MyRefinement
refine String do
def shout; upcase + "!"; end
end
end
Master these Ruby interview questions to confidently tackle technical interviews across experience levels. Practice coding examples hands-on for best results!