Get ready for your next interview with our comprehensive question library
nil
object responds true to .nil?
nil.nil? # true
"".empty? # true
[].empty? # true
" ".blank? # true (Rails only)
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
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
.to_s
.to_s
.inspect
puts "Hello" # Hello\n (returns nil)
print "Hello" # Hello (returns nil)
p "Hello" # "Hello"\n (returns "Hello")
class MyClass
def public_method; end
protected
def protected_method; end
private
def private_method; end
end
numbers = [1, 2, 3]
numbers.each { |n| n * 2 } # Returns [1, 2, 3]
numbers.map { |n| n * 2 } # Returns [2, 4, 6]
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 }
[[1, 2], [3, [4]]].flatten # [1, 2, 3, 4]
[1, nil, 2, nil, 3].compact # [1, 2, 3]
MVC separates application into three components:
The flow: Request → Router → Controller → Model → Controller → View → Response
Both ensure all developers and deployment environments use identical gem versions.
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
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess 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