[Ruby] Class nesting, inheritance, and the basics of self

Introduction

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.

Class inheritance

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.

Target to inherit

--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 Since 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. Also, since 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

Nesting classes, etc.

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

Differences in behavior depending on how it is defined

Constant reference

・ 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

Clarify whether to nest in class or module

・ 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

Whether to throw an exception when there is no object to nest from

・ 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)

end

Recommended Posts

[Ruby] Class nesting, inheritance, and the basics of self
[Ruby] Summary of class definitions. Master the basics.
[Java] Inheritance and structure of HttpServlet class
Basics of Ruby
[Ruby basics] About the role of true and break in the while statement
Ruby on Rails ~ Basics of MVC and Router ~
About next () and nextLine () of the Scanner class
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
[For beginners] DI ~ The basics of DI and DI in Spring ~
I summarized the types and basics of Java exceptions
[Technical memo] About the advantages and disadvantages of Ruby
Docker monitoring-explaining the basics of basics-
[GCD] Basics of DispatchQueue class
Understand the basics of docker
The basics of Swift's TableView
I tried to summarize the basics of kotlin and java
[Ruby] Questions and verification about the number of method arguments
The nth and n + 1st characters of a Ruby string
[Ruby] Creating code using the concept of classes and instances
[Ruby] Display today's day of the week using Date class
Basics of conditional branching and return
About the basics of Android development
Basics of sending Gmail in Ruby
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
[Ruby] See the essence of ArgumentError
The basics of SpringBoot + MyBatis + MySQL
[Ruby basics] split method and to_s method
This and that of the JDK
[Ruby] Date, Time, Datetime class basics
Various methods of the String class
Read the first 4 bytes of the Java class file and output CAFEBABE
Basics of Ruby ~ Review of confusing parts ~
About object-oriented inheritance and about yield Ruby
[Ruby] Review about nesting of each
Ruby Basics 2 ~ Review of confusing parts ~
[Ruby] Display the contents of variables
Easy to understand the difference between Ruby instance method and class method.
[Ruby] I want to extract only the value of the hash and only the key
[Ruby] Let's examine the inheritance tree while watching the flow of method search
Display Japanese calendar and days of the week using java8 standard class
[Challenge CircleCI from 0] Learn the basics of CircleCI
Understand the basics of Android Audio Record
[Ruby] From the basics to the inject method
Now, I've summarized the basics of RecyclerView
part of the syntax of ruby ​​on rails
Folding and unfolding the contents of the Recyclerview
About the operation of next () and nextLine ()
Explanation of Ruby Time and Date objects
[day: 5] I summarized the basics of Java
[Ruby] Cut off the contents of twitter-ads
Ruby from the perspective of other languages
Looking back on the basics of Java
Proper use of interface and abstract class
[Ruby] Classification and usage of loops in Ruby
Comparison of JavaScript objects and Ruby classes
About the mechanism of the Web and HTTP
Basics of java basics ② ~ if statement and switch statement ~
Ruby basics
Ruby basics
Call a method of the parent class by explicitly specifying the name in Ruby
Get the class name and method name of Controller executed by HandlerInterceptor of Spring Boot