About attr_accessor

What is an instance variable?

Variables shared inside the same instance

python


class Food
  def initialize(name)
    #Save the name passed when creating the instance in the instance variable
   @name = name
  end

  def eat
  "I will eat an #{@name}"
  end
end
food = Food.new('apple')
food.hello #=> "I will eat an apple"

Instance variables cannot be referenced from outside the class!

python


class Food
  def initialize(name)
   @name = name
  end

  #@Method for referencing name from the outside
  def name
  @name
  end
end
food = Food.new('apple')
#Via the name method@Get the contents of name
food.name #=> "apple"

If you want to change the contents of an instance variable from the outside, define a method for changing

python


class Food
  def initialize(name)
   @name = name
  end

  #@Method for referencing name from the outside
  def name
  @name
  end

 #@Method for changing name from the outside
  def name=(value)
    @name = value
  end
end
food = Food.new('apple')
#It looks like you're assigning it to a variable, but it's actually a name=You are calling a method.
food.name = 'banana'
#Via the name method@Get the contents of name
food.name #=> "banana"

A method that reads and writes the value of an instance variable is called an "accessor method". Writing these methods one by one is a hassle. That's where the "attr_accessor method" comes in.

attr_accessor method

python


class Food
  #@Methods to read and write name are automatically defined
  attr_accessor :name

  def initialize(name)
   @name = name
  end

  #@Method for referencing name from the outside
  #def name
  #@name
  #end

 #@Method for changing name from the outside
  #def name=(value)
    #@name = value
  #end
end
food = Food.new('apple')
#@Change name
food.name = 'banana'
#@Refer to name
food.name #=> "banana"

"Attr_reader" if you want to make the contents of the instance variable read-only If you want to write only, "attr_writer"

reference An introduction to Ruby for those who want to become professionals

Recommended Posts

About attr_accessor
About =
About method.invoke
About Kotlin
About Hinemos
About inheritance
About params
About Docker
About Rails 6
About form_for
About Spring ③
About enum
About polymorphism
About Optional
About hashes
About JitPack
About Dockerfile
About this ()
About devise
About encapsulation
About Docker
About JAVA_HOME
About active_hash
About static
About exceptions
About scope
[Maven] About Maven
About exception handling
Review about bundler
About Java interface
[Java] About Java 12 features
About Rails routing
About cyclomatic complexity
About exception handling
About AsyncTask Generics
About Ruby symbols
About array multiplication
[Java] About arrays
About HotSpot VM
About ruby ​​form
[Swift] About generics
About Active Storage
About class inheritance.
About Spring AOP
About Ruby Hashes
About singular methods
About getter setter
About build tools
About MacinCloud introduction
[Note] About nil
About keyword arguments
Chew about API
[About double hash]
About installing Cocoapods
Something about java
Where about java
About HttpServlet () methods
About Java features
About ActiveRecord :: PendingMigrationError
About SIGNING_REGION: NoSuchFieldError
About the method