Ruby accessor method

What is an accessor method?

A method that reads and writes the value of an instance variable. Ruby is designed so that instance variables cannot be accessed as it is.

(Example)

class Animal
  @name = ""
end

animal = Animal.new
animal.name = "cat"
puts animal.name # => ruby.rb:6:in `<main>': undefined method `name=' for #<Animal:0x00007fce4a8b4b40> (NoMethodError)

According to this code definition method, @name is defined as a different variable called "class instance variable", which is inaccessible from the instance.

How should I define the instance variables that can be accessed from the instance?

So we use the "accessor method".

First, there are three types of accessor methods.

attr_reader attr_reader is a getter for accessing instance variables.

(Example)

class Animal
  attr_reader :name 
  def initialize(name)
    @name = name
  end
end

animal = Animal.new("cat") 
puts animal.name # => cat

In this way, you can access the instance variable @name by writing attr_reader: name. However, as it is, the setter is not defined, so @name cannot be changed from outside the class.

attr_writer attr_writer is a setter for accessing instance variables.

(Example)

class Animal
  attr_writer :name
  attr_reader :name
  def initialize(name)
    @name = name
  end
end

animal = Animal.new("cat")
puts animal.name
animal.name = "dog"
puts animal.name 
# => cat
#    dog

In this way, by writing attr_writer: name in addition to attr_reader: name, you can change and access the instance variable @name from the outside.

attr_accessor This method combines the functions of attr_reader and attr_writter.

(Example)

class Animal
  attr_accessor :name
  def initialize(name)
    @name = name
  end
end
animal = Animal.new("cat")
puts animal.name
animal.name = "dog"
puts animal.name
# => cat
#    dog

In this way, you can change the instance variable @name from the outside and access it.

Recommended Posts

Ruby accessor method
Ruby to_s method
[Ruby] slice method
[Ruby] end_with? method
[Ruby] Method memorandum
Ruby build method
ruby map method
Ruby Learning # 30 Initialize Method
abbreviation for ruby method
Ruby Learning # 24 Exponent Method
Glossary: Accessor / Accessor Method, Encapsulation
definition of ruby method
[Ruby] Method definition summary
Method
Ruby algorithm (inject, method definition)
[Ruby] Notes on gets method
[Ruby] Method that returns truth
[ruby] Method call with argument
Ruby design pattern template method pattern memo
[Ruby] Method to count specific characters
[Ruby] present/blank method and postfix if.
[Ruby] Extracting elements with slice method
[Ruby basics] split method and to_s method
[Ruby] How to use any? Method
[Ruby] Search problem using index method
[Ruby on Rails] Convenient helper method
[Ruby] undefined method `dark?'occurs in rqr_code
How to use Ruby inject method
Ruby learning 4
[Ruby] Array
Java method
[Ruby] Obtaining even values ​​using the even? Method
to_i method
Ruby learning 5
Ruby basics
java (method)
Ruby Review 2
getRequestDispatcher () method
Ruby addition
Refactoring Ruby
Ruby on Rails installation method [Mac edition]
Ruby learning 3
Implemented "Floyd Cycle Detection Method" in Ruby
Map method
include method
Abstract method
Ruby print puts p printf output method
initialize method
List method
puts method
Ruby setting 2
ruby Exercise Memo II (variable and method)
Java method
Class method
Ruby problem ⑦
save! method
Ruby learning 2
[Java] method
[Ruby] Block
Refactoring Ruby
Ruby learning 6