Call a method of the parent class by explicitly specifying the name in Ruby

Reprinted from Blog Article.

Call its own parent method

You can use super to call a parent method with the same name as itself.

  def value
    "Super#value"
  end
end

class Sub < Super
  def value
    # Super#Call value
    super
  end
end

p Sub.new.value
# => "Super#value"

So what if you want to call an arbitrary parent method from a different method?

class Super
  def value
    "Super#value"
  end
end

class Sub < Super
  def value
    "Sub#value"
  end

  def value2
    #Here Super#I want to call value
  end
end

Answer: Use Method # super_method

In this case you can use Method # super_method. Get any method object as follows, and get the parent method object with #super_method.

class Super
  def value
    "Super#value"
  end
end

class Sub < Super
  def value
    "Sub#value"
  end

  def value2
    #Get a method object of value and reference its parent method
    method(:value).super_method.call
  end
end

p Sub.new.value2
# => "Super#value"

bonus

You can also use .instance_method to call a method of any class as shown below.

class Super
  def value
    "Super#value"
  end
end

class Sub < Super
  def value
    "Sub#value"
  end

  def value2
    Super.instance_method(:value).bind(self).call
  end
end

p Sub.new.value2
# => "Super#value"

Recommended Posts

Call a method of the parent class by explicitly specifying the name in Ruby
I tried to make a parent class of a value object in Ruby
Get the class name and method name of Controller executed by HandlerInterceptor of Spring Boot
I want to call a method of another class
super doesn't just call the methods of the parent class
[Ruby / Rails] Set a unique (unique) value in the class
[Java] Integer information of characters in a text file acquired by the read () method
Write a test by implementing the story of Mr. Nabeats in the world with ruby
Provisional memo when the name of the method parameter of the Java class cannot be obtained by reflection in the Eclipse plug-in project.
How to get the class name / method name running in Java
Determine that the value is a multiple of 〇 in Ruby
In Ruby you can define a method with any name
[Ruby] Get in the habit of using the dup method when making a copy of a string variable
Call the super method in Java
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
Create a calendar from the Calendar class by specifying the year and month
[Ruby] Specify the argument name of the method. Meaning of the colon (:) at the end
I checked the package name referenced by the class contained in the JAR file
Sort by multiple fields in the class
How to reference a column when overriding the column name method in ActiveRecord
Part of the class called from the class under test in JMockit Mock method memo
Be careful when setting the class name when creating a document-based app in Swift
Get the path defined in Controller class of Spring boot as a list
Cause of is not visible when calling a method of another class in java
[Order method] Set the order of data in Rails
[Ruby] Summary of class definitions. Master the basics.
Preventing mistakes in the Logger name by copying
[Java] Handling of JavaBeans in the method chain
[Java] Dynamic method call by reflection of enum (enum)
Java method call from RPG (method call in own class)
Measure the size of a folder in Java
Recommendation of Service class in Ruby on Rails
A quick review of Java learned in class
Method name of method chain in Java Builder + α
Create a native extension of Ruby in Rust
Class in Ruby
The presence or absence of! In Ruby method names does not mean destructive / non-destructive
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
[ruby] How to assign a value to a hash by referring to the value and key of another hash
Count the frequency of occurrence of words in a sentence by stream processing (Apache Apex)
A review of the code used by rails beginners
[Ruby] Cut out a string using the slice method
Format of the log output by Tomcat itself in Tomcat 8
[Kotlin] Get the argument name of the constructor by reflection
Return the execution result of Service class in ServiceResponse class
[Ruby] The role of subscripts in learning elements in arrays
Examine the elements in the array using the [Ruby] includes? Method
Write a class in Kotlin and call it in Java
Calculate the difference between numbers in a Ruby array
How to mock a super method call in PowerMock
A quick review of Java learned in class part3
Ruby, Nokogiri: Get the element name of the selected node
A quick review of Java learned in class part2
[Ruby] Class nesting, inheritance, and the basics of self
Get the URL of the HTTP redirect destination in Ruby
Why the get method is needed in the Calendar class
Summarize the additional elements of the Optional class in Java 9
[Ruby] Thinking when there is no receiver in front of the method (it looks like)
wsimport error handling (A class / interface with the same name "xxx" is already in use)
definition of ruby method
[Ruby] Count an even number in an array using the even? Method