What is the range of influence of inheritance while working? .. And What does self mean in this case? .. And I could only understand the basic part fluffy, so this time
--Class --Inheritance - self --Nest
I learned while actually writing, so I will write it as a record. I'd like to write methods and variables as well, but I'd like to write them separately because it seems to be long.
You can specify one superclass when defining a class, If not specified at this time, the object class is automatically inherited. Therefore, whenever you define a new class, it always inherits some class. It will always be somewhere in the inheritance tree.
--Superclass class methods --Instance method --Instance variables described in the method
You can also refer to superclass constants and class variables, although they are not inherited.
class Klass VALUE = "I'm VALUE" @@class_val = "I'm class_val" end class A < Klass end class B < A def self.display_bal puts @@class_val end end #Superclass constant reference puts B::VALUE # => I'm VALUE #Inheritance of class variables B.display_val # => I'm class_val
self refers to the object itself, its contents will change depending on where you use it.
In the example below, we pointed to the
instance object only when used within an instance method.
self used when defining a class method refers to a class object
You can also specify the
self of the
self. method name directly in the class name.
class Klass # Klass p self # Klass def (p self).class_method puts "inner_class_method" # Klass p self end def instance_method puts "inner_instance_method" # <Klass:0x00007fa07e1a47c8> p self end end Klass.class_method klass_instanse = Klass.new klass_instanse.instance_method
When you define a class, you can nest it in other classes. There are the following two patterns as the definition method, but some behaviors will change.
#Write directly in class TopKlass class Klass end end ## ------ ## # ”::Use class TopKlass end class TopKlass::Klass end
・ Describe directly in We will refer to the constants in order from ourselves to the top level. The flow is to refer to the top level in order from the closest to itself, and finally to the top level.
VALUE = "Top level" class TopKlass VALUE = "in TopKlass" class InnerKlass def self.put_value puts VALUE end end end TopKlass::InnerKlass.put_value # => in TopKlass
・ Describe using namespace If it doesn't have a constant, it then references the top level and then in order from there towards its own class.
VALUE = "Top level" class TopKlass VALUE = "in TopKlass" end class TopKlass::InnerKlass def self.put_value puts VALUE end end TopKlass::InnerKlass.put_value # => Top level
・ Described in Since class or module is specified in each hierarchy, an error will occur if a module defined separately is specified as class.
module TopKlass end class TopKlass class InnerKlass end end # => TopKlass is not a class (TypeError)
・ Describe using namespace Since it is only the one described at the end (far right) that specifies whether it is a class or not Other than that, it makes the judgment of class and module flexible.
module TopKlass end class TopKlass::InnerKlass end #No error
・ Described in In the example below, if TopKlass was not defined separately No exception will occur because TopKlass is newly defined.
class TopKlass class InnerKlass end end
・ Describe using namespace
In the following cases, an exception will be thrown if TopKlass is not defined separately. It doesn't define a new TopKlass.
class TopKlass::InnerKlass end # => uninitialized constant TopKlass (NameError)