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?
Ruby's attr_accessor is not an accessor
What is Docker?
What is null? ]
What is java
What is Keycloak
What is Jackson?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is `docker-compose up`?
What is a constructor?
What is hard coding?
What is a stream
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 Guava's @VisibleForTesting?
What is MVC model?
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?
[Swift] What is dismiss?
[Java] What is flatMap?
What is a Servlet?
What is web development?
[Java] What is JavaBeans?
[Android] What is Context? ??
[Java] What is ArrayList?
[Ruby] What is true?
Understanding ruby's "|| =" specification and what is Rails presence method?
What is object-oriented after all?
What is HttpSession session = request.getSession ();
What is Java Assertion? Summary.
What is DI (Dependency Injection)
What is a wrapper class?
What is object-oriented programming? ~ Beginners ~
What is a boolean type?