All questions
Showing of 91What is the difference between nil, empty, and blank in Ruby?
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:
Last attempt -
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.
nilis the single object ofNilClass. Onlynilreturnstruefornil?, andnilhas noempty?method.empty?is defined onString,ArrayandHash. It istrueonly when the length is zero, so" ".empty?isfalse.blank?comes from Rails' Active Support, not plain Ruby. It istruefornil,falseand 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)
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 ↓
Explain the difference between single quotes and double quotes in Ruby strings.
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:
Last attempt -
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
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 ↓
What are symbols in Ruby and when should you use them?
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:
Last attempt -
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
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 ↓
What is the difference between puts, print, and p in Ruby?
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:
Last attempt -
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")
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 ↓
What are the three levels of access control for methods in Ruby?
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:
Last attempt -
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
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 ↓
What is the difference between Array#map and Array#each?
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:
Last attempt -
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]
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 ↓
Explain the difference between Hash#merge and Hash#merge!
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:
Last attempt -
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 }
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 ↓
Explain the difference between Array#flatten and Array#compact.
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:
Last attempt -
flatten removes nesting, while compact removes nil values. They solve different problems and are often chained.
flattenturns 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]].compactreturns a copy without thenilelements. It keepsfalseand empty strings, because onlynilis 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]
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 ↓
Explain the Rails MVC architecture.
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:
Last attempt -
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.
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 ↓
What are Ruby's naming conventions?
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:
Last attempt -
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.
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 ↓
What is the purpose of Gemfile and Gemfile.lock?
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:
Last attempt -
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.
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 ↓
What is the safe navigation operator (&.) and when should you use it?
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:
Last attempt -
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
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 ↓
What do attr_accessor, attr_reader, and attr_writer do?
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:
Last attempt -
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
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 ↓
What is the difference between a class and a module in Ruby?
What is the difference between Array#select, Array#reject, and Array#partition?
What is the rightward assignment operator in Ruby 3?
Explain the difference between == and === in Ruby.
Explain the concept of Duck Typing in Ruby.
What is the difference between instance variables and class variables?
Explain method visibility inheritance in Ruby.
What is the purpose of the super keyword in Ruby?
What is method aliasing and when would you use it?
How does the Enumerable module work in Ruby?
What is the spaceship operator (<=>) and how is it used?
What is the difference between yield and block.call?
Explain the & operator in method parameters.
What is method chaining and how do you implement it?
What are keyword arguments and how do they differ from positional arguments?
What is the splat operator (*) and double splat (**)?
What is the difference between include, prepend, and extend?
What is the purpose of module_function?
How do you handle module name conflicts?
What is the difference between raise and throw in Ruby?
Explain the structure of begin-rescue-else-ensure-end.
How do you create custom exception classes?
What is the difference between File.open and File.new?
How do you handle file operations safely in Ruby?
What is the difference between require and load?
Explain the difference between system, exec, and backticks for running shell commands.
What is the difference between stub and mock in testing?
What is the purpose of fixtures and factories in testing?
What debugging techniques are available in Ruby?
What is the difference between render and redirect_to in Rails?
What are Rails callbacks and when should you use them?
What is the N+1 query problem and how do you solve it?
What is the difference between concerns and services in Rails?
Explain strong parameters in Rails.
What is the difference between has_many :through and has_and_belongs_to_many?
Explain database transactions in Rails.
What are scopes in ActiveRecord and how do they differ from class methods?
Explain the difference between delete and destroy in ActiveRecord.
What is the difference between lazy and eager evaluation?
How do you securely handle sensitive data in Ruby?
What is the Singleton pattern and how is it implemented in Ruby?
What is the difference between composition and inheritance?
What is the Law of Demeter and how does it apply to Ruby?
What is the difference between fail-fast and fail-safe approaches?
What is pattern matching in Ruby and how does it work?
What are endless method definitions in Ruby 3?
What are numbered block parameters?
Explain keyword argument separation in Ruby 3.0
What is the ... (three dots) operator for argument forwarding?
What is the difference between then, yield_self, and tap?
What is the difference between ==, eql?, and equal? in Ruby?
What is the difference between dup and clone in Ruby?
What does self refer to in Ruby?
What does freeze do, and what is frozen_string_literal?
When would you use Struct or Data in Ruby?
What is the difference between rescue StandardError and rescue Exception?
What is the purpose of Rails middleware?
What is optimistic locking in Rails?
What is Rack and why is it important?
Explain the concept of Dependency Injection in Ruby.
What is the difference between a block, proc, and lambda?
Explain the Ruby method lookup chain.
What are the common performance optimization techniques in Ruby?
How do you profile Ruby code performance?
What is a memory leak in Ruby, and how do you prevent it?
What is metaprogramming in Ruby?
Explain method_missing and how to use it safely.
What is the difference between class_eval and instance_eval?
What are singleton methods and singleton classes?
What is the Global Interpreter Lock (GIL) in Ruby?
What is the difference between Thread and Fiber in Ruby?
How do you handle thread safety in Ruby?
What are common security vulnerabilities in Ruby applications?
Explain the SOLID principles in the context of Ruby.
How does Ruby's garbage collection work?
What are refinements in Ruby?
What are Ractors and how do they enable true parallelism?
What are Fiber Scheduler and async I/O in Ruby 3?
This answer is part of Pro.
The full written answer, with the trade-offs and follow-ups an interviewer will probe.
No matches
Try a different filter or search term.
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.
Ruby cheatsheet
- Strings, symbols & equality01
- Arrays, hashes & Enumerable02
- Blocks, procs & lambdas03
- Arguments & pattern matching04
- Classes, self & visibility05
- Mixins & metaprogramming06
- Errors, files, shell & gems07
- Threads, memory & speed08
- Rails & Active Record09
- Design, tests & security10
- The model11
- + 5 more inside
This picture is part of Pro. The first chapter is free to play; the other 28 pictures come with Pro.
Get Pro · $10/mo Already Pro? Sign in78 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.
MEAN
MongoDB, Express, Angular, Node.jsMERN
MongoDB, Express, React, Node.jsDjango
Python Full-Stack DevelopmentRuby on Rails
Convention over ConfigurationServerless on AWS
Serverless Architecture on AWSInterviewers also test these - they're common to every stack, whichever one you picked above.
Flutter Mobile
Flutter Cross-Platform Mobile DevelopmentInterviewers also test these - they're common to every stack, whichever one you picked above.
Spring Boot
Enterprise Java Development.NET
Microsoft EcosystemVue
Vue.js, Vite, TypeScript, Tailwind, Node.jsGo Backend
Golang, gRPC, PostgreSQL, Redis, RabbitMQInterviewers also test these - they're common to every stack, whichever one you picked above.
FastAPI
Python, FastAPI, SQLAlchemy, PostgreSQLReact Native
React, TypeScript, Redux, FirebaseiOS Native
Swift, SwiftUI, UIKit, FirebaseAndroid Native
Java, Jetpack Compose, FirebaseDevOps / Platform
Docker, Kubernetes, Terraform, CI/CDInterviewers also test these - they're common to every stack, whichever one you picked above.
AI Engineer
LLMs, RAG, Agents, EvalsAI-Powered Developer
Claude Code, Copilot, Agentic WorkflowsCore SWE Interview Prep
Data structures, algorithms, OS, concurrency, networking, gitInterviewers also test these - they're common to every stack, whichever one you picked above.
Interviewers also test these - they're common to every stack, whichever one you picked above.