Test your understanding of Ruby's modern features including pattern matching, refinements, frozen string literals, numbered parameters, and safe navigation.
data = { type: "user", id: 123, profile: { name: "Alice" } }
result = case data
in { type: "user", id: id, profile: { name: name } }
"User #{name} with ID #{id}"
in { type: "admin", permissions: perms }
"Admin with #{perms.length} permissions"
else
"Unknown data type"
end
puts result
What will this pattern matching code output?
module StringExtensions
refine String do
def palindrome?
self == self.reverse
end
end
end
def check_without_using
"racecar".palindrome?
end
def check_with_using
using StringExtensions
"racecar".palindrome?
end
puts check_without_using
puts check_with_using
What happens when this code runs?
# frozen_string_literal: true
str1 = "Hello"
str2 = "Hello"
str3 = "Hello".dup
puts str1.object_id == str2.object_id
puts str1.frozen?
puts str3.frozen?
str1 << " World"
What will happen when this code runs?
pairs = [["a", 1], ["b", 2], ["c", 3]]
result1 = pairs.map { |letter, number| "#{letter}#{number}" }
result2 = pairs.map { "#{_1[0]}#{_1[1]}" }
result3 = pairs.map { "#{_1}#{_2}" }
puts result1 == result2
puts result1 == result3
What will this code output?
users = [
{ name: "Alice", profile: { bio: "Developer" } },
{ name: "Bob", profile: nil },
{ name: "Carol" },
nil
]
result = users.map { |user| user&.dig(:profile, :bio)&.upcase }
puts result.inspect
What will this code output?
def process_coordinates(coords)
case coords
in [x, y] if x > 0 && y > 0
"First quadrant"
in [0, 0]
"Origin"
in [x, *rest] if rest.length > 1
"Multi-dimensional: #{rest.length + 1}D"
in [x, y]
"2D point"
else
"Unknown"
end
end
puts process_coordinates([3, 4])
puts process_coordinates([1, 2, 3, 4])
puts process_coordinates([-1, 5])
What will this code output?
What is the main advantage of refinements over traditional monkey patching?
# frozen_string_literal: true
array1 = 1000.times.map { "constant_string" }
array2 = 1000.times.map { "constant_string".dup }
puts "Array1 unique object_ids: #{array1.map(&:object_id).uniq.length}"
puts "Array2 unique object_ids: #{array2.map(&:object_id).uniq.length}"
What will this code output?
data = [1, 2, 3]
# Which of these will work correctly?
result1 = data.map { |x| _1 * 2 }
result2 = data.map { _1 * 2 }
result3 = data.each_with_index { puts "#{_2}: #{_1}" }
Which statement about this code is correct?
class User
attr_accessor :preferences
def initialize
@preferences = {}
end
end
user = User.new
nil_user = nil
result1 = user&.preferences&.[]=(theme: "dark")
result2 = nil_user&.preferences&.[]=(theme: "dark")
puts user.preferences
puts result1.inspect
puts result2.inspect
What will this code output?
ages = [15, 25, 65, 5]
results = ages.map do |age|
case age
in n if n < 13
"child"
in n if n < 20
"teen"
in n if n >= 65
"senior"
in n
"adult"
end
end
puts results.inspect
What will this code output?
# frozen_string_literal: true
module ArrayExtensions
refine Array do
def safe_first
self&.first
end
end
end
using ArrayExtensions
data = [
[1, 2, 3],
[],
nil
]
result = data.map { _1&.safe_first || "empty" }
This code combines multiple modern Ruby features. What does it demonstrate?
Watch and learn modern features quiz