What is Ruby's attr_accessor?

class Example
  attr_accessor :a, :b 
  def initialize(a,b)
    @a = a
    @b = b
  end
end

@ex = Example.new("A", "B")
@ex.a # =>"A" 

attr_accessor is a method that defines the setter and getter methods of instance variables that match the argument symbols. In this case, "a" is the getter and "= a" is the setter. In other words, attr_accessor: a created the methods "a" and "= a".

@ex.methods.grep /a/ # => [:a.:a=,...]

Recommended Posts

What is Ruby's attr_accessor?
What is params
What is SLF4J?
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is RubyGem?
What is Byte?
What is Tomcat
What is Java Encapsulation?
What is permission denied?
What is instance control?
What is an initializer?
What is Spring Tools 4
What is an operator?
What is object orientation?
What is an annotation?
What is Java technology?
What is Java API-java
What is @ (instance variable)?
What is Gradle's Artifact?
What is JPA Auditing?
[Java] What is flatMap?
What is a Servlet?
What is web development?
[Java] What is ArrayList?
What is object-oriented after all?
What is a jar file?
What exactly is an API?
What is a Java collection?
What is a lambda expression?
[Ruby] What is `!!` used for?
[Java] What is jaee j2ee?
[Java] What is class inheritance?
What is the constructor for?
Introduction to Ratpack (1) --What is Ratpack?
What is a fa⁉ enum?
[Java basics] What is Class?
What is java escape analysis?
What is Microservices? And Microservices Frameworks
What is Rails Active Record?
What is the initialize method?
What is thread safe (with Java)
What is a column Boolean type?
What is it? ~ 3 types of "no" ~
What is a reference type variable?
What is @Autowired in Spring boot?
[Technical Note] What is Object Oriented?
[Folio LSP] What is Okapi-Stripes Platform?
What is a lambda expression (Java)
What kind of method is define_method?
What is a Ruby 2D array?
What is the best file reading (Java)
What is a class in Java language (1 /?)
[For super super beginners] What is object orientation?
What is Java and Development Environment (MAC)
What is a class in Java language (2 /?)
What is the model test code testing
What is the main method in Java?
What is the data structure of ActionText?