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] Correctly Handle . versus :: Syntax #4564

Open
DavidBakerEffendi opened this issue May 16, 2024 · 0 comments
Open

[ruby] Correctly Handle . versus :: Syntax #4564

DavidBakerEffendi opened this issue May 16, 2024 · 0 comments
Labels
ruby Relates to rubysrc2cpg

Comments

@DavidBakerEffendi
Copy link
Collaborator

Context

When a receiver is explicitly specified in a method invocation, it may be separated from the method name using either a period (.) or two colons (::). The only difference between these two forms occurs if the method name starts with an uppercase letter. In this case, Ruby will assume that a receiver::Thing method call is actually an attempt to access a constant called Thing in the receiver unless the method invocation has a parameter list between parentheses.

Example:

irb(main):021:0> module A
irb(main):022:1>   CONST = 0
irb(main):023:1>   def foo
irb(main):024:2>     puts "hhelo"
irb(main):025:2>   end
irb(main):026:1> end
=> :foo
irb(main):027:0> A::CONST
=> 0
irb(main):028:0> 
irb(main):029:0> A::foo
Traceback (most recent call last):
        4: from /usr/bin/irb:23:in `<main>'
        3: from /usr/bin/irb:23:in `load'
        2: from /Library/Ruby/Gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        1: from (irb):29
NoMethodError (undefined method `foo' for A:Module)

However

irb(main):030:0> module B
irb(main):031:1>   foo = 0
irb(main):032:1> end
=> 0
irb(main):033:0> B::foo
Traceback (most recent call last):
        4: from /usr/bin/irb:23:in `<main>'
        3: from /usr/bin/irb:23:in `load'
        2: from /Library/Ruby/Gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        1: from (irb):33
NoMethodError (undefined method `foo' for B:Module)

But

irb(main):064:0> module B
irb(main):065:1>   def self.foo
irb(main):066:2>     puts "helo"
irb(main):067:2>   end
irb(main):068:1> end
=> :foo
irb(main):069:0> B::foo
helo
=> nil

Right now we approximate all of the above as calls, which is largely fine, except in the case of the constant access.

Problem

We are not testing include as an inheritance operation, nor are we making use of the heuristic that the capitalization of the member has any significance and can instead be a simple field access.

@DavidBakerEffendi DavidBakerEffendi added the ruby Relates to rubysrc2cpg label May 16, 2024
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

1 participant