Here, ruby class variables, instance variables, accessors, etc. I will write the basic part.
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
end
class Klass < SuperKlass
@@class_var = :class_val
def self.class_method
puts "class_method called"
puts @@super_klass_var
puts @@class_var
end
def instance_method
puts "instance_method called"
puts @@super_klass_var
puts @@class_var
end
end
klass_instance = Klass.new
Klass.class_method
# => class_method called
# => super_klass_val
# => class_val
klass_instance.instance_method
# => instance_method called
# => super_klass_val
# => class_val
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
end
father = Famiry.new
father.name
# => nil
father.name = "Namihey"
father.name
# => Namihey
If attr_accessor is not used, a NoMethodError will occur.
class Famiry
end
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
end
def tukkomi
puts @val
end
end
east = Geinin.new("why")
east.tukkomi
north = Geinin.new("why")
north.tukkomi
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
@name
end
#Assignment method
def name=(val)
@name = val
end
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
#Definition
@class_instance_val = :class_instance_val
def self.class_method
#reference
@class_instance_val
end
end
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