[Ruby] Basic knowledge of class instance variables, etc.


Here, ruby class variables, instance variables, accessors, etc. I will write the basic part.

Class variables

The class itself, the subclass, Variables that can be referenced from class methods and instance methods.

To define it, add @@ at the beginning.

class SuperKlass
  @@super_klass_var = :super_klass_val

class Klass < SuperKlass
  @@class_var = :class_val
  def self.class_method
      puts "class_method called"
      puts @@super_klass_var
      puts @@class_var

  def instance_method
      puts "instance_method called"
      puts @@super_klass_var
      puts @@class_var

klass_instance = Klass.new
  # => class_method called
  # => super_klass_val
  # => class_val
  # => instance_method called
  # => super_klass_val
  # => class_val

Instance variables

By holding a value in an instance variable, it is possible to hold an object-specific state. Note that instance variables cannot be accessed directly from outside the object. You need to use the ʻattr_accessor` method to access it.

class Famiry
  attr_accessor :name

father = Famiry.new

# => nil
father.name = "Namihey"
# => Namihey

If attr_accessor is not used, a NoMethodError will occur.

class Famiry

father = Famiry.new

father.name = "Namihey"
# => undefined method `name=' for #<Famiry:0x00007f8a97908bd8> (NoMethodError)

If you define it by prefixing it with @ in the instance method, it becomes an instance variable.

class Geinin
  def initialize(val)
    @val = val

  def tukkomi
    puts @val


east = Geinin.new("why")

north = Geinin.new("why")

attr_accessor This method allows reference and assignment for instance variables. ʻAttr_reader to deny assignments and allow references only ʻAttr_writer to deny references and allow only assignments Etc. are used properly. Such a method is called a accessor.

The following methods are defined by using the above methods.

#Reference method
def name

#Assignment method
def name=(val)
  @name = val

Class instance variable

Since a class is an object of the Class class, the class object itself You can have instance variables.

In the context where self points to a class, just like the instance variables above If you define it with @ at the beginning, it becomes a class instance variable.

You can also refer to it in the context where self points to the class.

class klass
  @class_instance_val = :class_instance_val
  def self.class_method

Differences between class variables and class instance variables

Class variables and class instance variables may feel the same, Because class instance variables are instance variables of class objects It cannot be referenced within the instance method defined in that class.

Also, class variables can be referenced and changed from a class that inherits that class. Class instance variables are not allowed. In summary, the following two points are different.

--Reference from instance method --Class variables ... can --Class instance variable ... I can't --Reference and change from inherited class --Class variables ... can --Class instance variable ... I can't


Recommended Posts

[Ruby] Basic knowledge of class instance variables, etc.
[Ruby] Class methods, instance methods, etc.
Basic knowledge of Ruby on Rails
[Ruby] Classes, instance variables, instances, etc ...
[Basic knowledge of Java] Scope of variables
[Ruby] methods, instance methods, etc ...
Ruby: Differences between class methods and instance methods, class variables and instance variables
Basic methods of Ruby hashes
Basic knowledge of SQL statements
Basic methods of Ruby arrays
[Self-use memo] (Ruby) class, class instance, instance
[Ruby] List of basic commands
Review of Ruby basic grammar
[Ruby] Handle instance variables with instance methods
[Ruby] Display the contents of variables
Basic knowledge
[Java] Differences between instance variables and class variables
Basic knowledge of Java development Note writing
[Basic knowledge of Java] About type conversion
Java engineers now compare to learn the basic grammar of Ruby Part 1 (Basic, Variables)
Instance concept, class type variables, constructors, static members
Summary of basic knowledge of Rails acquired by progate
Recommendation of Service class in Ruby on Rails
Ruby basic terms
Class in Ruby
Basics of Ruby
Java basic knowledge 1
Variables / scope (ruby)
About Ruby variables
Introduction to Java for beginners Basic knowledge of Java language ①
[Ruby] Class nesting, inheritance, and the basics of self
[Java Silver] What are class variables instance variables and local variables?