Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ruby] Method Full Name Standard #4500

Open
DavidBakerEffendi opened this issue Apr 29, 2024 · 1 comment
Open

[ruby] Method Full Name Standard #4500

DavidBakerEffendi opened this issue Apr 29, 2024 · 1 comment
Labels
ruby Relates to rubysrc2cpg

Comments

@DavidBakerEffendi
Copy link
Collaborator

The method full name of Ruby is a bit of a mixture of other frontends. Something like ::program and <module> are specific to JavaScript and Python's interpreters specifically. This should be investigated and changed to what Ruby's standard actually uses.

@DavidBakerEffendi DavidBakerEffendi added the ruby Relates to rubysrc2cpg label Apr 29, 2024
@badly-drawn-wizards badly-drawn-wizards self-assigned this Apr 30, 2024
@DavidBakerEffendi
Copy link
Collaborator Author

ChatGPT gave me a useful explanation that could help here. TDLR our :program could be main (even though main is global, it is "more correct" than :program)

  1. Define the Method
def foo
    puts "Hello, world!"
end

This code defines a method foo at the top level, which means it is defined on the main object. In Ruby, when you define methods outside of any class or module, they are actually private methods of the Object class. The main object is the default object context at the top level and is an instance of Object.

  1. Call the Method
foo

When you call foo at the top level, Ruby looks for the method foo in the current context, which is main.

Detailed Dispatch Process

  1. Receiver Context:

Since foo is called without an explicit receiver, Ruby treats it as a method call on self. At the top level, self is the main object.

  1. Method Lookup:

Ruby starts by checking if foo is a method of the main object. Since main is an instance of Object, Ruby checks Object for a private method foo.
It finds foo as a private method of Object, because it was defined at the top level.

  1. Invoke the Method:

The method foo is invoked, and it executes the code within the method body:

puts "Hello, world!"
  1. Output:

The method foo executes and outputs "Hello, world!" to the console.

Summary

In this specific case, the method call dispatching is quite straightforward:

  • The method foo is defined as a private method on the Object class.
  • When foo is called at the top level, Ruby resolves this call to the foo method on self, which is main.
  • Since main is an instance of Object, Ruby finds the foo method in Object's private methods and invokes it, resulting in the output "Hello, world!".
  • This process is a demonstration of how Ruby handles method definitions and calls at the top level, leveraging the main object and the implicit context provided by self.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ruby Relates to rubysrc2cpg
Projects
None yet
Development

No branches or pull requests

2 participants