Ruby Logo

Ruby Accessors & Attributes Mastery

Master Ruby accessors: attr_reader, attr_writer, attr_accessor, and custom attribute methods.

Home Ruby Ruby Accessors & Attributes Mastery

Ruby Accessors & Attributes Mastery

Ruby's accessor methods provide elegant ways to define getter and setter methods for instance variables. Master attr_reader, attr_writer, attr_accessor, and custom attribute methods for clean, maintainable code.

Basic Accessor Methods

Accessor Methods: Ruby provides three built-in accessor methods to automatically generate getter and setter methods for instance variables.

attr_reader - Read-Only Access

# attr_reader creates getter methods only
class
Person
attr_reader :name, :age
def initialize(name, age)
@name = name
@age = age
end
end

person = Person.new("Alice", 25)
puts
person.name
# "Alice"
puts
person.age
# 25
# person.name = "Bob" # NoMethodError!

attr_writer - Write-Only Access

# attr_writer creates setter methods only
class
Config
attr_writer :api_key, :timeout
def initialize
@api_key = "default"
@timeout = 30
end
end

config = Config.new
config.api_key = "secret123"
config.timeout = 60
# puts config.api_key # NoMethodError!

attr_accessor - Read/Write Access

# attr_accessor creates both getter and setter methods
class
User
attr_accessor :name, :email, :active
def initialize(name, email)
@name = name
@email = email
@active = true
end
end

user = User.new("Alice", "alice@example.com")
puts
user.name
# "Alice"
user.name = "Alice Smith"
user.active = false
puts
user.active
# false

Custom Accessor Methods

Custom Accessors: Override default accessor behavior with validation, formatting, and custom logic.

Custom Getter with Formatting

# Custom getter with formatting
class
Product
attr_reader :name
def initialize(name, price)
@name = name
@price = price
end
def price
"$%.2f" % @price
end
end

product = Product.new("Laptop", 999.99)
puts
product.price
# "$999.99"

Custom Setter with Validation

# Custom setter with validation
class
BankAccount
attr_reader :balance
def initialize(initial_balance)
@balance = initial_balance
end
def balance=(amount)
raise "Balance cannot be negative" if amount < 0
@balance = amount
end
end

account = BankAccount.new(1000)
account.balance = 1500
puts
account.balance
# 1500
# account.balance = -100 # RuntimeError!

Interactive Practice: Accessors & Attributes

Practice Time: Try these accessor and attribute examples. Understanding these patterns will make you a more effective Ruby programmer.

Interactive Code Runner

Ruby Code Editor
Output will appear here when you run the code...

Best Practices & Common Patterns

✅ Best Practices

  • Use attr_reader for read-only attributes
  • Use attr_writer for write-only attributes
  • Use attr_accessor for read/write attributes
  • Override accessors for validation and formatting
  • Use virtual attributes for computed values
  • Keep accessor methods simple and focused

❌ Common Pitfalls

  • Overusing attr_accessor without validation
  • Not handling edge cases in custom accessors
  • Creating circular dependencies in accessors
  • Forgetting to call super in overridden accessors
  • Making accessors too complex
  • Not documenting custom accessor behavior

Accessors & Attributes Mastery Summary

You've Mastered Ruby Accessors & Attributes!

Built-in Accessors

attr_reader, attr_writer, attr_accessor

Custom Accessors

Validation, formatting, computed values

Virtual Attributes

Computed properties and derived values

Ruby accessors provide elegant ways to manage object state. Use them wisely to create clean, maintainable interfaces while adding validation and formatting where needed.

Quick Navigation

Related Topics

Video Tutorial

Watch and learn ruby accessors & attributes mastery

Pro Tip: After reading through the content above, watch this video to reinforce your understanding and see the concepts in action!