# Method Lookup Path

In order to know where to look for a method, Ruby uses a defined lookup path. This lookup path is, in order:

1. Inside the class
2. Inside the last included module (then the one before, etc)
3. In the superclass (if inheriting)
4. In the superclass’ modules (if included, starting with the last)
5. In the Object class
6. In the Kernel class
7. In the BasicObject class

The method lookup path can be checked with the ancestors class method:

module Moving
end

module Sleeping
end

module Swim
end

module Fly
end

class Animal
include Moving
include Sleeping
end

class Flamingo < Animal
include Swim
include Fly

def speak
puts 'Fancy cry!'
end
end

my_flamingo = Flamingo.new
my_flamingo.speak # => Fancy cry!

puts Flamingo.ancestors
# => Flamingo
#    Fly
#    Swim
#    Animal
#    Sleeping
#    Moving
#    Object
#    Kernel
#    BasicObject

The important thing to note is the order in which we include modules is important. Last module is looked at first. The same thing happens for each superclass until reaching BasicObject.

Every class in Ruby inherits from the Object class, which itself inherits from Kernel, which itself inherits form BasicObject.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.21999205648899078, "perplexity": 17162.67986935174}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 20, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2022-05/segments/1642320303779.65/warc/CC-MAIN-20220122073422-20220122103422-00320.warc.gz"}