[Ruby] Thinking when there is no receiver in front of the method (it looks like)


The receiver is A in A.medhod (B). It may appear that there is no receiver in front of the method. For example, in the following cases.

class ClassA
  def say
    puts instance_of? String #false
    puts instance_of? ClassA #true

instance_of? [Ruby] Difference between is_a? And instance_of? As I wrote in the explanation of

A.instance_of? B 

At one point, A is an instance of B, but in the above example, ʻA.` is missing, isn't it?

Way of thinking

In conclusion, the instance_of? Above is preceded by an instance of Class A. When referencing a method of the same class, inheritance class or included module, it is not necessary to precede the method with an instance (I think it is automatically attached). Addendum: I received a comment that "there is a rule that self is the receiver for method calls that do not explicitly write the receiver", but I think this way of thinking is easier to understand.

For example, in the following cases,

class ClassB
  def say_b
  def say_b2
    puts "bbb"
# bbb

This doesn't feel strange, right? Methods of the same class can be executed from other methods without writing (or even writing) a receiver. Similarly, the instance_of? Method above is actually a method of the Object class of the superclass (strictly speaking, it seems to be a method of the Kernel module included in the Object class).

=> Kernel

Cause of feeling uncomfortable

Perhaps the reason I felt uncomfortable when the first instance_of? Suddenly appeared was because I couldn't see the relationship between the instance_of? Method and Class A.

Confirmation method

You can also check the receiver.


You can output with.

class ClassB
  def say_b
    puts method(:say_b2).receiver
  def say_b2
    puts "bbb"
# bbb
# #<ClassB:0x00007ff34d8a0550>

You can see that the receiver of say_b2 is an instance of ClassB. Actually, there is already an instance of ClassB before method (: say_b2).

Recommended Posts

[Ruby] Thinking when there is no receiver in front of the method (it looks like)
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
Data acquisition method memo when there is HashMap in HashMap
[Ruby] Get in the habit of using the dup method when making a copy of a string variable
When there is no output to stdout in docker log
Determine that the value is a multiple of 〇 in Ruby
Throw an exception and catch when there is no handler corresponding to the path in spring
Like function The part that is stuck in making it asynchronous
How to output the value when there is an array in the array
When I edit the properties file, it looks like garbled characters
What is @Override or @SuppressWarnings ("SleepWhileInLoop") in front of the function? ?? ??
Investigation method when the CPU of the server running java is heavy
What is it? ~ 3 types of "no" ~
Error when the member of Entity class used in SpringWebFlux is final
Initial value when there is no form object property in Spring request
[Ruby] How to prevent errors when nil is included in the operation
[Swift] Rather, it is meaningful to avoid var and declare it with let only when there is a branch in the assignment of the initial value.
What is the main method in Java?
When reassigning to an argument in a Ruby method and then calling `super` → The reassigned one is used
If it is Ruby, it is efficient to make it a method and stock the processing.
Call a method of the parent class by explicitly specifying the name in Ruby
Is it mainstream not to write the closing tag of <P> tag in Javadoc?
Cause of is not visible when calling a method of another class in java
[Java] When putting a character string in the case of a switch statement, it is necessary to make it a constant expression
[Order method] Set the order of data in Rails
[Java] Handling of JavaBeans in the method chain
Behavior when wild card (**) is specified in ruby
The ruby version is managed in the .rbenv / version file
When the project is not displayed in eclipse
The presence or absence of! In Ruby method names does not mean destructive / non-destructive