[Ruby] Method to easily get the receiver type .class

This is a personal memo.

Class which refers to the class (type) of the receiver and superclass which confirms the parent class

Also about Basic Object.

table of contents

  1. .class
  2. .superclass
  3. .BasicObject

1 .class

You can easily get the receiver class (type) by using the .class method.

python


x = 100

x.class 
=> Integer

python


str = "text"

str.class
=> String

python


obj = {:a=>1, :b=>2, :c=>3}

obj.class
=> Hash

python


arr = [1, 2, 3]

arr.class
=> Array

python


nil.class
=> NilClass

python


(1..4).class
=> Range

python


(1.23).class
=> Float

python


Class.class
=> Class

Class is an unnamed class. Called a metaclass. The class created with the class class name end has this Class.

python


class TestClass
  def hello
    msg = "test class"
    p "hello #{msg}"
  end
end

TestClass.class
=> Class

python


Kernel.class
=> Module

Kernel is a module that defines methods that can be referenced by all classes.

python


module TestModule
  def helloModule
    p "hello Module"
  end
end

TestModule.class
=> Module

2 .superclass You can refer to the superclass using the superclass method.

It cannot be used for elements such as numbers, strings, arrays, hashes, nil, and modules.

It can be used for the type obtained by the class method.

What is a superclass?

The parent class is called the superclass.

python


class AAA
end

#Inherit class AAA
class BBB < AAA
end

When the class is inherited as described above, ** the parent class AAA becomes the superclass **.

BBB is called a subclass.

Class subclass name <superclass name

For Numeric

python


100.class
=> Integer

100.class.superclass
=> Numeric

100.class.superclass.superclass
=> Object

100.class.superclass.superclass.superclass
=> BasicObject

100.class.superclass.superclass.superclass.superclass
=> nil

Integer's superclass is Numeric. Numeric's superclass is Object.

python


#Error when used for numbers
100.superclass
NoMethodError (undefined method `superclass' for 100:Integer)

### For class

python


class Aaa
end

Aaa.class
=> Class

Aaa.superclass
=> Object

Object returns the superclass of Class.

When inherited

python


class Aaa
end

class Bbb < Aaa
end

BBB.superclass
=> Aaa

For module

You cannot use superclass for module.

python


module Xxx
end

Xxx.class
=> Module

Xxx.superclass
=> NoMethodError (undefined method 'superclass' for Xxx:Module)

3 .BasicObject BasicObject is the top-level concept of a class. A class that has the minimum necessary functions such as class identification and method call.

Hierarchical structure of classes


BasicObject
↓
Object
↓
String, Numeric, Module, Class,,,
            ↓
     Integer, Floot

In Ruby, all data is called an Object, and underneath it is a string of strings and a Numeric of numbers.

The higher you go, the more abstract and simple the definition becomes. BasciObject is the class at that time.


The basicObject superclass is nil.

python


 BasicObject.superclass
=> nil

Recommended Posts

[Ruby] Method to easily get the receiver type .class
How to get the class name / method name running in Java
Easy to understand the difference between Ruby instance method and class method.
To get Ruby Silver at the last minute
I want to get the value in Ruby
[Ruby basics] How to use the slice method
[Ruby] How to calculate the total amount using the initialize method and class variables
Feel the basic type and reference type easily with ruby
Feel the basic type and reference type easily with ruby 2
[Ruby] How to get the tens place and the ones place
Why the get method is needed in the Calendar class
Easily try Ruby 3.0.0 type checking (rbs)
How to use the form_with method
[Ruby] Method to count specific characters
How to use the wrapper class
[Ruby] How to use any? Method
I tried to explain the method
Method to add the number of years and get the end of the month
[For beginners] How to get the Ruby delayed railway line name
[Java] How to convert from String to Path type and get the path
[Ruby on Rails] Use the resources method to automatically create routes.
How to use Ruby inject method
Get the type of an array element to determine if it is an array
[Java] How to get to the front of a specific string using the String class
Iterative processing of Ruby using each method (find the sum from 1 to 10)
How to get date data in Ruby
[Ruby] Obtaining even values ​​using the even? Method
[Kotlin] 3 ways to get Class from KClass
[Java] How to use the HashMap class
[Rails] How to use the map method
[Java] How to use the toString () method
I wanted to add @VisibleForTesting to the method
[Java] How to get the current directory
I was addicted to the roll method
[Processing × Java] How to use the class
How to get the date in java
Get the error message using the any? method
[Java] How to use the Calendar class
[Java] Get the date with the LocalDateTime class
If it is Ruby, it is efficient to make it a method and stock the processing.
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
Call a method of the parent class by explicitly specifying the name in Ruby
[Java] How to easily get the longest character string of ArrayList using stream
I tried to get the distance from the address string to the nearest station with ruby
Get the class name and method name of Controller executed by HandlerInterceptor of Spring Boot
Class method
I want to get only the time from Time type data ...! [Strftime] * Additional notes
How to get the date from the JavaScript Date type that C # developers are addicted to
I want to recursively get the superclass and interface of a certain class
[Ruby] How to use the map method. How to process the value of an object and get it by hash or symbol.
How to find the cause of the Ruby error
[Ruby] Summary of class definitions. Master the basics.
How to get keycloak credentials in interceptor class
Override protected method with anonymous class to stub
A memorandum to clean up the code Ruby
[Ruby] How to use gsub method and sub method
The story of introducing Ajax communication to ruby
[Ruby] I want to do a method jump!
How to get Class from Element in Java
How to get today's day of the week
When you want to use the method outside