Interview Questions

Get ready for your next interview with our comprehensive question library

Ruby Interview Questions

Filter by Difficulty

1.

What is the difference between nil, empty, and blank in Ruby?

beginner
  • nil: Represents "nothing" or "no value". Only nil object responds true to .nil?
  • empty: A method that returns true for collections with no elements (arrays, hashes, strings)
  • blank: Rails addition that returns true for nil, empty collections, and whitespace-only strings
nil.nil?        # true
"".empty?       # true
[].empty?       # true
" ".blank?      # true (Rails only)
2.

Explain the difference between single quotes and double quotes in Ruby strings.

beginner

Double quotes allow string interpolation and escape sequences, while single quotes treat most characters literally (except for \' and \\).

name = "Alice"
puts "Hello #{name}"   # "Hello Alice"
puts 'Hello #{name}'   # "Hello #{name}"
puts "Line\nBreak"     # Line with actual break
puts 'Line\nBreak'     # "Line\nBreak" literally
3.

What are symbols in Ruby and when should you use them?

beginner

Symbols are immutable, reusable constants represented internally by an integer value. They're often used as hash keys, method names, or status identifiers because they're memory-efficient - the same symbol always references the same object in memory.

:status.object_id == :status.object_id  # true
"status".object_id == "status".object_id  # false
# Common usage
user = { name: "John", age: 30 }  # symbols as hash keys
4.

What is the difference between puts, print, and p in Ruby?

beginner
  • puts: Outputs value with newline, returns nil, calls .to_s
  • print: Outputs value without newline, returns nil, calls .to_s
  • p: Outputs inspected value with newline, returns the value, calls .inspect
puts "Hello"    # Hello\n (returns nil)
print "Hello"   # Hello (returns nil)
p "Hello"       # "Hello"\n (returns "Hello")
5.

What are the three levels of access control for methods in Ruby?

beginner
  • public: Can be called from anywhere (default)
  • protected: Can be called by instances of the same class or subclasses
  • private: Can only be called within the same instance (no explicit receiver)
class MyClass
  def public_method; end
  
  protected
  def protected_method; end
  
  private
  def private_method; end
end
6.

What is the difference between Array#map and Array#each?

beginner
  • each: Iterates through elements, returns original array, used for side effects
  • map: Transforms elements, returns new array with transformed values
numbers = [1, 2, 3]
numbers.each { |n| n * 2 }  # Returns [1, 2, 3]
numbers.map { |n| n * 2 }   # Returns [2, 4, 6]
7.

Explain the difference between Hash#merge and Hash#merge!

beginner
  • merge: Returns a new hash with combined key-value pairs
  • merge!: Modifies the original hash in place (destructive)
h1 = { a: 1, b: 2 }
h2 = { b: 3, c: 4 }
h1.merge(h2)   # Returns { a: 1, b: 3, c: 4 }, h1 unchanged
h1.merge!(h2)  # h1 is now { a: 1, b: 3, c: 4 }
8.

Explain the difference between Array#flatten and Array#compact.

beginner
  • flatten: Removes nested array structure, creating a single-level array
  • compact: Removes nil values from array
[[1, 2], [3, [4]]].flatten  # [1, 2, 3, 4]
[1, nil, 2, nil, 3].compact  # [1, 2, 3]
9.

Explain the Rails MVC architecture.

beginner

MVC separates application into three components:

  • Model: Handles data and business logic (ActiveRecord)
  • View: Presents data to users (ERB templates)
  • Controller: Coordinates between Model and View, handles requests

The flow: Request → Router → Controller → Model → Controller → View → Response

10.

What are Ruby's naming conventions?

beginner
  • Classes/Modules: CamelCase (UserAccount)
  • Methods/Variables: snake_case (user_name)
  • Constants: SCREAMING_SNAKE_CASE (MAX_USERS)
  • Predicates: end with ? (empty?)
  • Dangerous methods: end with ! (save!)
11.

What is the purpose of Gemfile and Gemfile.lock?

beginner
  • Gemfile: Specifies gem dependencies and sources
  • Gemfile.lock: Locks specific versions for consistent environments

Both ensure all developers and deployment environments use identical gem versions.

12.

What is the safe navigation operator (&.) and when should you use it?

beginner

The safe navigation operator (&.) calls methods only if the receiver is not nil, preventing NoMethodError on nil objects.

user&.profile&.name  # Returns nil if any part is nil
# Equivalent to:
user && user.profile && user.profile.name

# With arrays
users&.first&.name  # Safe even if users is nil
13.

What is the rightward assignment operator in Ruby 3?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
14.

Explain the difference between == and === in Ruby.

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
15.

What is the difference between a class and a module in Ruby?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
16.

Explain the concept of Duck Typing in Ruby.

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
17.

What is the difference between instance variables and class variables?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
18.

Explain method visibility inheritance in Ruby.

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
19.

What is the purpose of super keyword in Ruby?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
20.

What is method aliasing and when would you use it?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
Showing 1 to 20 of 85 results

Premium Plan

$10.00 /monthly
  • Access all premium content - interview questions, and other learning resources

  • We regularly update our features and content, to ensure you get the most relevant and updated premium content.

  • 1000 monthly credits

  • Cancel anytime