In order not to confuse the understanding of getters and setters, [Do not use accessors for anything! ]

There are several getter and setter commentary articles, so I'll leave that to you. Also, there is a convenient code called Accessor, but I didn't have enough information about how to use it properly, so I wrote it as a basis for beginners.

First of all, it is a place to design a common car.

If you can use accessors

Car design


class Car
  attr_accessor :model, :color, :option
  def initialize(model, color, option)
    @model = model
    @color = color
    @option = option
  end
end

car = Car.new("Benz", "white", "wheel")

#=>All can be changed

If this is the case, you can put it together with attr_accessor, For example, ** "I'm already making a car, but I'm in trouble if I can change the color with the car model !!! (I wish it was only an option ...)" **

When using getters and setters

Car design


class Car
  attr_reader :model, :color, :option
  attr_writer :option 
  def initialize(model, color, option)
    @model = model
    @color = color
    @option = option
  end
end

car = Car.new("Benz", "white", "wheel")

#=>Only options can be changed

It's common to write that, If you want to write smarter with accessors

Smart to use accessors and getters

Car design


class Car
  attr_accessor :option
  attr_reader :model, :color 
  def initialize(model, color, option)
    @model = model
    @color = color
    @option = option
  end
end

car = Car.new("Benz", "white", "wheel")

#=>Only options can be changed

Will be.

By the way,

When I try to change the model of the car ...

Car design


class Car
  attr_accessor :option
  attr_reader :model, :color 
  def initialize(model, color, option)
    @model = model
    @color = color
    @option = option
  end
end

car = Car.new("Benz", "white", "wheel")

car.model = "BMW"
#=>NoMethodError (undefined method `model=' for #<Car:0x00007fc009900440>)

I get angry like that! !! ** "Don't change the car model! I'm already making it!" ** That's right. Attempting to change the color will result in a similar error.

Summary

So, I looked back on how to use getters, setters, and accessors for Ruby beginners to study. I hope it will be helpful for beginners. Thank you very much! !!

Recommended Posts

In order not to confuse the understanding of getters and setters, [Do not use accessors for anything! ]
Use hashes well in Ruby to calculate the total amount of an order
Do you use the for statement after all? Do you use a while statement? Proper use of for statement and while statement
[For beginners] DI ~ The basics of DI and DI in Spring ~
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
One of the causes and solutions when whales do not appear in Docker Quickstart Terminal
Do not use floats and doubles to calculate decimal numbers
Reintroduction to Java for Humanities 0: Understanding the Act of Programming
What to do when "relation" hibernate_sequence "does not exist" in the ID column of PostgreSQL + JPA
[Note] What to do if bundle install in Chapter 3 of the rails tutorial is not possible
If you want to mock a method in RSpec, you should use the allow method for mock and the singleton method.
[Java] Get the dates of the past Monday and Sunday in order
What to do when the changes in the Servlet are not reflected
Spring validation was important in the order of Form and BindingResult
[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]
[Java] Do not use "+" in append!
Order of processing in the program
What to do if the prefix c is not bound in JSP
The story of forgetting to close a file in Java and failing
Set the number of seconds for fast forward and rewind in ExoPlayer
Confirmation and refactoring of the flow from request to controller in [httpclient]
(Determine in 1 minute) About the proper use of empty ?, blank? And present?
How to change the maximum and maximum number of POST data in Spark
How to constrain the action of the transition destination when not logged in
I tried to make full use of the CPU core in Ruby
[Rails] The cause of not being able to post was in form_with
[Rails] Articles for beginners to organize and understand the flow of form_with
I want you to use Enum # name () for the Key of SharedPreference
Is it possible to put the library (aar) in the Android library (aar) and use it?
In the topic of "total concentration", "How to use Docker" is summarized for the virtual Mameko who sleeps in me.
Do not use instance variables in partial
must not return in the for statement
What to do if the changes are not reflected in the jar manifest file
What to do when rails db: seed does not reflect in the database
You may not want to use the remove method in ArrayList very often
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
Is it mainstream not to write the closing tag of <P> tag in Javadoc?
What to do when ‘Could not find’ in any of the sources appears in the development environment with Docker × Rails × RSpec
[Java] What to do if the contents saved in the DB and the name of the enum are different in the enum that reflects the DB definition
[Swift] If the support of the application is iOS 11 or later, it was not necessary to use Int and Int64 properly