[go: up one dir, main page]

DEV Community

Cover image for Ruby's attr_reader and attr_writer
Jennifer Tran
Jennifer Tran

Posted on

Ruby's attr_reader and attr_writer

Imagine you are writing a program to help your local cat animal shelter, so you start by defining a Cat Class in Ruby 😺

class Cat
  def initialize(name)
    @name = name
  end

  def name
    @name
  end
end
Enter fullscreen mode Exit fullscreen mode

Now you go ahead and make a call to create a new Cat with a name:

cat = Cat.new
cat.name = "Telemakhos" #=> NoMethodError
Enter fullscreen mode Exit fullscreen mode

You run into NoMethodError 🙀. Well, that is expected since we haven't written a method to define a cat's name. We've just written the getter method. We also need to write a _setter_method.

What is a getter and setter method?

Getter methods are used to retrieve the value of an instance variable from an object and Setter methods are used to set the value of an instance variable. In many languages, you can read and write the value of an instance variable directly but in Ruby you have to specify a getter and setter.

Let's have a second pass at our Cat Class. Say you create a new instance of Cat attached to a variable tabby:

class Cat
  def initialize(name)
    @name = name
  end

  def name #getter method
    @name
  end

  def name=(name) #setter method
    @name = name
  end
end

tabby = Cat.new('Telemakhos')
puts tabby.name #=> "Telemakhos"
Enter fullscreen mode Exit fullscreen mode

With our new getter and setter methods, we can now get and set the name of a cat 😼

Now imagine if you had more properties to a Cat than just name

class Cat
  def initialize(name, age, breed, weight)
    @name = name
    @age = age
    @breed = breed
    @weight = weight
  end
end
Enter fullscreen mode Exit fullscreen mode

If you wanted to get or set any of those properties, it would require eight more getter and setter methods! 😿

There is a better alternative: attr_reader and attr_writer!

attr_reader and attr_writer are Ruby methods that can be used to replace getter and setter methods and mimic the accessing of instance variables seen in other languages such as TypeScript. Using attr_reader and attr_writer is especially useful when you want to read and manipulate the values of a Class that has many instance variables.

Here is an example of it's utility in depth:

# cat.rb
class Cat
  attr_reader :name, :age, :breed, :weight
  attr_writer :name, :age, :breed, :weight
  def initialize(name, age, breed, weight)
    @name = name
    @age = age
    @breed = breed
    @weight = weight
  end
end

cat = Cat.new("Julie", 3, "Tabby", 8)
puts cat.name # => "Julie"
puts cat.age # => 3
puts cat.breed # => "Tabby"
puts cat.weight # => 8
cat.weight = 10
puts cat.weight # => 10

Enter fullscreen mode Exit fullscreen mode

In conclusion, attr_reader and attr_writer can replace a bunch of getter and setter methods in just a single line! 😻

Top comments (2)

Collapse
 
leastbad profile image
leastbad

Don't forget attr_accessor, which creates both!

BTW, I love your header image so much.

Collapse
 
botanical profile image
Jennifer Tran

Yes, great point!! Thank you 😄