loading...
Cover image for Ruby's attr_reader and attr_writer

Ruby's attr_reader and attr_writer

botanical profile image Jennifer Tran ใƒป2 min read

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

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

cat = Cat.new
cat.name = "Telemakhos" #=> NoMethodError

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"

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

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

In conclusion, attr_reader and attr_writer can replace a bunch of getter and setter methods in just a single line! ๐Ÿ˜ป

Posted on Apr 21 by:

botanical profile

Jennifer Tran

@botanical

(she/her) | engineer who loves tofu scramble, books, and laughing

Discussion

markdown guide
 

Don't forget attr_accessor, which creates both!

BTW, I love your header image so much.

 

Yes, great point!! Thank you ๐Ÿ˜„