LearnThatStack Ace your next interview
Backend Development

Ruby.

Start free Change topic Change
Practice · Questions

All questions

Showing of 91
Beginner 15
01

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

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

nil means no value at all. empty? asks whether a string or collection has no elements, and blank? is a Rails check that also treats nil and whitespace as empty.

  • nil is the single object of NilClass. Only nil returns true for nil?, and nil has no empty? method.
  • empty? is defined on String, Array and Hash. It is true only when the length is zero, so " ".empty? is false.
  • blank? comes from Rails' Active Support, not plain Ruby. It is true for nil, false and empty collections. A string of only whitespace is blank too.

Plain Ruby treats only nil and false as falsy. An empty string and 0 are truthy, which is why blank? is useful for form input.

nil.nil?        # true
"".empty?       # true
[].empty?       # true
" ".blank?      # true (Rails only)
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

02

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

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Double quotes process string interpolation and escape sequences, while single quotes keep almost every character exactly as written.

In a double-quoted string, #{name} runs the code inside the braces and inserts the result. Escapes such as \n for a newline and \t for a tab turn into those characters.

In a single-quoted string, #{name} and \n stay as plain text. The only escapes are \' for a quote and \\ for a backslash.

Both forms build the same kind of String object. Use double quotes when the string needs a value or a special character inside it, and single quotes for fixed text.

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
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

03

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

Visual

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain
Visual

A symbol is an immutable name written with a leading colon, like :status. Every use of the same symbol refers to the same object in memory.

Ruby stores each symbol once and represents it internally by an integer ID. That saves memory and makes comparing two symbols fast, because Ruby only checks identity.

Use symbols for hash keys, method names and fixed labels such as :active or :pending. Use strings for text that changes or comes from users. Without the frozen_string_literal magic comment, two identical string literals are two separate objects, so the second comparison below is false.

: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
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

04

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

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

puts and print write the to_s text of a value and return nil, while p writes the inspect form and returns the value itself.

puts adds a newline after each value, and print adds none. Given an array, puts prints each element on its own line.

p shows the developer view of a value. Strings keep their quotes, and nil appears as nil, where puts nil prints an empty line. Because p returns its argument, you can put it in the middle of an expression while debugging and the result does not change.

puts "Hello"    # Hello\n (returns nil)
print "Hello"   # Hello (returns nil)
p "Hello"       # "Hello"\n (returns "Hello")
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

05

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

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Ruby has public, protected and private methods. Public is the default.

  • Any code can call a public method.
  • Code inside the same class or a subclass can call a protected method, even on another object. One object can then compare itself with another of its kind.
  • Only the object itself can call a private method, with no receiver, meaning no object before the dot. Since Ruby 2.7, a literal self. also works.

send can still call a private method from outside, so visibility is not a security boundary.

class MyClass
  def public_method; end
  
  protected
  def protected_method; end
  
  private
  def private_method; end
end
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

06

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

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

each runs the block for its side effects and returns the original array, while map collects the block's results into a new array.

Use each when something should happen for every element, such as printing or saving it. Use map when every element should become a new value.

A common mistake is to call each and expect the transformed values back. each ignores what the block returns, so the numbers.each line below still returns [1, 2, 3].

Neither method changes the original array. The bang version map! replaces the elements in place.

numbers = [1, 2, 3]
numbers.each { |n| n * 2 }  # Returns [1, 2, 3]
numbers.map { |n| n * 2 }   # Returns [2, 4, 6]
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

07

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

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

merge returns a new hash and leaves the original unchanged, while merge! is destructive, which means it changes the hash you call it on.

Both combine the key-value pairs of the two hashes. When a key exists in both, the value from the argument wins, so :b becomes 3 below. A block, if you pass one, runs only for those shared keys and picks the value, as in h1.merge(h2) { |key, old, new| old + new }.

Use merge when other code still needs the original hash. Use merge! when you own the hash and want to avoid building a copy. The trailing ! is Ruby's convention for the version that changes the object, and update is another name for merge!.

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 }
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

08

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

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

flatten removes nesting, while compact removes nil values. They solve different problems and are often chained.

  • flatten turns nested arrays into one flat array, at every depth by default. Pass a depth to stop early, so [[1, [2]]].flatten(1) gives [1, [2]].
  • compact returns a copy without the nil elements. It keeps false and empty strings, because only nil is removed.

When a map block returns arrays, flat_map maps and flattens one level in a single step.

Both return a new array. The bang versions flatten! and compact! change the array in place and return nil when nothing changed.

[[1, 2], [3, [4]]].flatten  # [1, 2, 3, 4]
[1, nil, 2, nil, 3].compact  # [1, 2, 3]
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

09

Explain the Rails MVC architecture.

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Rails splits an app into models, views and controllers, so data rules, page markup and request handling each live in their own place.

  • The model is an Active Record class. It holds the data, the validations and the business rules, and it maps to a database table.
  • The view is a template, usually ERB, that turns data into HTML or JSON.
  • The controller receives the request, asks models for data, and then renders a view or redirects to another URL.

A request reaches the router first. The router calls a controller action, the action works with models, and the controller sends back the rendered view as the response.

Keeping business rules in models, out of views and controllers, makes them easier to test and reuse.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

10

What are Ruby's naming conventions?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Ruby uses snake_case for methods and variables, CamelCase for classes and modules, and SCREAMING_SNAKE_CASE for constants, like user_name, UserAccount and MAX_USERS.

A capital first letter makes a name a constant. A prefix sets a variable's scope: @ for instance variables, @@ for class variables and $ for globals.

Method names add two suffixes by convention. A name ending in ? is a predicate that answers true or false, like empty?. A name ending in ! marks a more dangerous version of a method, like save!. It often changes the object it is called on or raises an error.

File names use snake_case and match the class inside, so UserAccount lives in user_account.rb.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

11

What is the purpose of Gemfile and Gemfile.lock?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

The Gemfile lists the gems a project needs, where to get them, and the version ranges it accepts. Gemfile.lock records the exact version Bundler installed for every gem, including the gems those gems depend on.

When you run bundle install and the lock file still matches the Gemfile, Bundler installs exactly those versions. It skips dependency resolution, the step that picks one version of each gem to fit every range. That keeps every developer machine, build server and production host on the same gem versions. bundle update is the step that moves gems to newer versions and rewrites the lock file.

Commit both files for an application.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

12

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

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

The safe navigation operator &. calls a method only when the receiver is not nil. When the receiver is nil, it skips the call and returns nil instead of raising NoMethodError.

Use it where nil is an expected value, such as an optional association or a missing hash entry. Each step of a chain needs its own &.. Avoid long &. chains where nil would mean a bug, because they hide the error until later.

It is not exactly the same as &&. &. checks only for nil, so false&.to_s still calls the method. The && version below also calls user.profile twice.

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

# With arrays
users&.first&.name  # Safe even if users is nil
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

13

What do attr_accessor, attr_reader, and attr_writer do?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

They write accessor methods for instance variables, so you do not write simple getters and setters by hand. attr_reader :name defines a name method that returns @name, attr_writer :name defines a name= method that sets it, and attr_accessor :name defines both.

Pick the narrowest one. A value that callers should only read gets attr_reader, so the object stays in control of its own changes.

Inside the class, call a writer as self.name = value. Without self, Ruby creates a local variable instead.

class User
  attr_reader :id
  attr_accessor :name

  def initialize(id, name)
    @id = id
    @name = name
  end
end

user = User.new(1, "Ann")
user.name = "Bea"   # writer from attr_accessor
user.name           # "Bea"
user.id = 2         # NoMethodError: no id= writer
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

14

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

Part of Pro
15

What is the difference between Array#select, Array#reject, and Array#partition?

Part of Pro
Intermediate 58
16

What is the rightward assignment operator in Ruby 3?

Part of Pro
17

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

Part of Pro
18

Explain the concept of Duck Typing in Ruby.

Part of Pro
19

What is the difference between instance variables and class variables?

Part of Pro Visual
20

Explain method visibility inheritance in Ruby.

Part of Pro
21

What is the purpose of the super keyword in Ruby?

Part of Pro
22

What is method aliasing and when would you use it?

Part of Pro Visual
23

How does the Enumerable module work in Ruby?

Part of Pro Visual
24

What is the spaceship operator (<=>) and how is it used?

Part of Pro
25

What is the difference between yield and block.call?

Part of Pro
26

Explain the & operator in method parameters.

Part of Pro
27

What is method chaining and how do you implement it?

Part of Pro
28

What are keyword arguments and how do they differ from positional arguments?

Part of Pro
29

What is the splat operator (*) and double splat (**)?

Part of Pro
30

What is the difference between include, prepend, and extend?

Part of Pro
31

What is the purpose of module_function?

Part of Pro
32

How do you handle module name conflicts?

Part of Pro
33

What is the difference between raise and throw in Ruby?

Part of Pro
34

Explain the structure of begin-rescue-else-ensure-end.

Part of Pro
35

How do you create custom exception classes?

Part of Pro
36

What is the difference between File.open and File.new?

Part of Pro
37

How do you handle file operations safely in Ruby?

Part of Pro
38

What is the difference between require and load?

Part of Pro
39

Explain the difference between system, exec, and backticks for running shell commands.

Part of Pro
40

What is the difference between stub and mock in testing?

Part of Pro
41

What is the purpose of fixtures and factories in testing?

Part of Pro
42

What debugging techniques are available in Ruby?

Part of Pro
43

What is the difference between render and redirect_to in Rails?

Part of Pro Visual
44

What are Rails callbacks and when should you use them?

Part of Pro Visual
45

What is the N+1 query problem and how do you solve it?

Part of Pro Visual
46

What is the difference between concerns and services in Rails?

Part of Pro
47

Explain strong parameters in Rails.

Part of Pro
48

What is the difference between has_many :through and has_and_belongs_to_many?

Part of Pro
49

Explain database transactions in Rails.

Part of Pro
50

What are scopes in ActiveRecord and how do they differ from class methods?

Part of Pro
51

Explain the difference between delete and destroy in ActiveRecord.

Part of Pro
52

What is the difference between lazy and eager evaluation?

Part of Pro Visual
53

How do you securely handle sensitive data in Ruby?

Part of Pro
54

What is the Singleton pattern and how is it implemented in Ruby?

Part of Pro
55

What is the difference between composition and inheritance?

Part of Pro
56

What is the Law of Demeter and how does it apply to Ruby?

Part of Pro
57

What is the difference between fail-fast and fail-safe approaches?

Part of Pro
58

What is pattern matching in Ruby and how does it work?

Part of Pro
59

What are endless method definitions in Ruby 3?

Part of Pro
60

What are numbered block parameters?

Part of Pro
61

Explain keyword argument separation in Ruby 3.0

Part of Pro
62

What is the ... (three dots) operator for argument forwarding?

Part of Pro
63

What is the difference between then, yield_self, and tap?

Part of Pro
64

What is the difference between ==, eql?, and equal? in Ruby?

Part of Pro
65

What is the difference between dup and clone in Ruby?

Part of Pro Visual
66

What does self refer to in Ruby?

Part of Pro
67

What does freeze do, and what is frozen_string_literal?

Part of Pro
68

When would you use Struct or Data in Ruby?

Part of Pro
69

What is the difference between rescue StandardError and rescue Exception?

Part of Pro Visual
70

What is the purpose of Rails middleware?

Part of Pro Visual
71

What is optimistic locking in Rails?

Part of Pro Visual
72

What is Rack and why is it important?

Part of Pro
73

Explain the concept of Dependency Injection in Ruby.

Part of Pro
Expert 18
74

What is the difference between a block, proc, and lambda?

Part of Pro Visual
75

Explain the Ruby method lookup chain.

Part of Pro
76

What are the common performance optimization techniques in Ruby?

Part of Pro
77

How do you profile Ruby code performance?

Part of Pro
78

What is a memory leak in Ruby, and how do you prevent it?

Part of Pro Visual
79

What is metaprogramming in Ruby?

Part of Pro
80

Explain method_missing and how to use it safely.

Part of Pro
81

What is the difference between class_eval and instance_eval?

Part of Pro Visual
82

What are singleton methods and singleton classes?

Part of Pro Visual
83

What is the Global Interpreter Lock (GIL) in Ruby?

Part of Pro Visual
84

What is the difference between Thread and Fiber in Ruby?

Part of Pro
85

How do you handle thread safety in Ruby?

Part of Pro Visual
86

What are common security vulnerabilities in Ruby applications?

Part of Pro
87

Explain the SOLID principles in the context of Ruby.

Part of Pro
88

How does Ruby's garbage collection work?

Part of Pro Visual
89

What are refinements in Ruby?

Part of Pro
90

What are Ractors and how do they enable true parallelism?

Part of Pro
91

What are Fiber Scheduler and async I/O in Ruby 3?

Part of Pro Visual

No matches

Try a different filter or search term.

Know someone prepping for Ruby? Send them this set.
Quick recap

Ruby in pictures.

A quick recap of Ruby in 33 short animated pictures, one idea at a time. About 8 min to get the whole topic back in your head before you walk in. Pause any picture, slow it down, or step through it.

Pro The first chapter is free to play. The other 28 pictures come with Pro.

Get Pro · $10/mo
Pro · $10/mo

78 of 91 Ruby answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime

Change topic

Pick a different technology or stack. Your current topic stays put until you choose a new one.

Technologies
No technologies match “”.
Cross-cutting topics
No topics match “”.
By role
Stacks & frameworks

MEAN

MongoDB, Express, Angular, Node.js

MERN

MongoDB, Express, React, Node.js

LAMP

Linux, Apache, MySQL, PHP

Django

Python Full-Stack Development

Ruby on Rails

Convention over Configuration

Serverless on AWS

Serverless Architecture on AWS

Flutter Mobile

Flutter Cross-Platform Mobile Development

Spring Boot

Enterprise Java Development

.NET

Microsoft Ecosystem

Vue

Vue.js, Vite, TypeScript, Tailwind, Node.js

Go Backend

Golang, gRPC, PostgreSQL, Redis, RabbitMQ

FastAPI

Python, FastAPI, SQLAlchemy, PostgreSQL

React Native

React, TypeScript, Redux, Firebase

iOS Native

Swift, SwiftUI, UIKit, Firebase

Android Native

Java, Jetpack Compose, Firebase

DevOps / Platform

Docker, Kubernetes, Terraform, CI/CD

AI Engineer

LLMs, RAG, Agents, Evals

AI-Powered Developer

Claude Code, Copilot, Agentic Workflows

Core SWE Interview Prep

Data structures, algorithms, OS, concurrency, networking, git