0
0
Rubyprogramming~20 mins

Attr_reader, attr_writer, attr_accessor in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Attribute Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using attr_reader?

Consider this Ruby class with attr_reader. What will be printed?

Ruby
class Person
  attr_reader :name
  def initialize(name)
    @name = name
  end
end

p = Person.new("Alice")
puts p.name
p.name = "Bob"
A
Alice
Bob
BNoMethodError: undefined method `name' for #<Person:...>
C
Alice
NoMethodError: undefined method `name=' for #&lt;Person:...&gt;
D
nil
NoMethodError: undefined method `name=' for #&lt;Person:...&gt;
Attempts:
2 left
💡 Hint

Remember, attr_reader creates a getter method but no setter.

Predict Output
intermediate
2:00remaining
What happens when using attr_writer only?

Look at this Ruby code using attr_writer. What will be the output or error?

Ruby
class Car
  attr_writer :color
  def initialize(color)
    @color = color
  end
end

car = Car.new("red")
puts car.color
car.color = "blue"
puts car.instance_variable_get(:@color)
ANoMethodError: undefined method `color' for #<Car:...>
BNoMethodError: undefined method `color=' for #<Car:...>
C
nil
blue
D
red
blue
Attempts:
2 left
💡 Hint

attr_writer creates a setter but no getter method.

Predict Output
advanced
2:00remaining
What is the output when using attr_accessor?

Given this Ruby class with attr_accessor, what will be printed?

Ruby
class Book
  attr_accessor :title
  def initialize(title)
    @title = title
  end
end

book = Book.new("Ruby 101")
puts book.title
book.title = "Ruby 102"
puts book.title
A
Ruby 101
Ruby 102
B
Ruby 101
Ruby 101
CNoMethodError: undefined method `title=' for #<Book:...>
DNoMethodError: undefined method `title' for #<Book:...>
Attempts:
2 left
💡 Hint

attr_accessor creates both getter and setter methods.

🧠 Conceptual
advanced
1:30remaining
Which statement about attr_reader, attr_writer, and attr_accessor is true?

Choose the correct statement about these Ruby attribute methods.

A<code>attr_reader</code> creates both getter and setter methods.
B<code>attr_accessor</code> creates getter and setter methods.
C<code>attr_writer</code> creates only a getter method.
D<code>attr_accessor</code> creates only a setter method.
Attempts:
2 left
💡 Hint

Think about which methods allow reading and writing.

🔧 Debug
expert
2:30remaining
Why does this Ruby code raise NoMethodError when using attr_writer?

Look at this code snippet. Why does puts user.name raise NoMethodError?

Ruby
class User
  attr_writer :name
  def initialize(name)
    @name = name
  end
end

user = User.new("Eve")
puts user.name
ABecause <code>attr_writer</code> creates only a getter method, no setter method exists.
BBecause <code>attr_writer</code> creates both getter and setter methods, but the getter is private.
CBecause the instance variable <code>@name</code> is not initialized.
DBecause <code>attr_writer</code> only creates a setter method, no getter method exists.
Attempts:
2 left
💡 Hint

Check what methods attr_writer creates.