[RUBY] About the ancestors method

There was useful information about the ancestors method in the book, so I summarized it.

Recognition so far



class Mountains
end

class Nepal  < Mountains
  def everest_place
    "nepal"
  end
end

  p Nepal.ancestors
  p Nepal.class.superclass.superclass.superclass

Output result



[Nepal, Mountains, Object, Kernel, BasicObject]
BasicObject

It is inconvenient to follow the inheritance relationship one by one with superclass. I realized that it would be convenient to use the ancestors method to retrieve everything from the current class to BasicObject as an array. (Including modules)

New recognition



module World
 def everest_place
  "china"
 end
end

class Mountains
 def everest_place
  "india"
 end
end

class Nepal < Mountains
  include World
 def everest_place
   "nepal"
 end
end

p Nepal.ancesters

nepal = Nepal.new
puts nepal.everest_place

as mentioned above, ❶ The Nepal class includes the World module, so you can use the Everest_place method of the World module. ❷ The Nepal class inherits the Mountains method, so you can also use the Everest_place method of the Moutains class.

** Suppose you have three everest_place methods, including the everest_place method in the Nepal class ** Of course, the output will change depending on which everest_place method is called.

The ancestors method is useful in such cases. Looking at the output of p Nepal.ancestors,

Nepal.Output result of ancestors


[Nepal, World, Mountains, Object, Kernel, BasicObject]

It will be displayed. ** Since it is called in order from the left, you can see that the everest_place method defined in the Nepal class is fetched. ** **

Let's actually see the result.

Output result


nepal

The return value of the everest_place method defined in the Nepal class has returned properly.

Recommended Posts

About the ancestors method
About the method
Output about the method # 2
About the authenticate method.
About the map method
About the to_s method.
Output about the method Part 1
about the where method (rails)
Consideration about the times method
About the role of the initialize method
About the package
About No Method Error
About the StringBuilder class
Commentary: About the interface
About the asset pipeline
About Java method binding
About the ternary operator
About method splitting (Java)
About the Kernel module
[Output] About the database
About the [ruby] operator
About the handling of Null
About specifying the JAXRS path
Note the StringUtils # containsAny method
What is the pluck method?
A note about the scope
About the description of Docker-compose.yml
[Java Silver] About equals method
Understand the helper method form_with
About the explanation about functional type
About the programming language Crystal
About Rails scraping method Mechanize
What is the initialize method?
How to use the link_to method
About the behavior of ruby Hash # ==
Method
About the language to be learned
How to use the include? method
How to use the form_with method
About the basics of Android development
[Rails] About the Punk List function
About the equals () and hashcode () methods
About the symbol <%%> in Rails erb
About the information sharing app band
Java comparison using the compareTo () method
About the current development environment (Java 8)
I tried to explain the method
A murmur about the utility class
[Rails] About helper method form_with [Basic]
Think about the 7 rules of Optional
About =
[Rails 6] destroy using the resources method
Was that so, the user_signed_in? method
Summary about the introduction of Device
About the log level of java.util.logging.Logger
Call the super method in Java
My thoughts on the equals method (Java)
About the version of Docker's Node.js image
Guess about the 2017 Java Persistence Framework (3) Reladomo
[Ruby] Obtaining even values ​​using the even? Method
Check once again about the OpenSSL command