Consider this class:
class Test1 puts self.t # => 2 def self.t 2 end end
It does print 2, even though the function is defined after the call. Since methods are not executed immediately, I see how it’d make sense if they were declared before any code in the class/module is run.
So, my question originally was what would happen if I tried to use any of the non-method code inside one of the methods. I came up with the following to completely confuse myself:
class Test @@val = f @@val = 4 def self.f @@val end def self.val @@val end end puts Test.val # => 4
This example does print 4. But I don’t understand how it does that given that the code is not organised in the following order:
fand assign the result to
How does the compiler distinguish between
@@val=f? Are methods really declared before any code is run, or does the compiler rely on the semantic stage to make sure the methods are present? Is there anything else I’m missing?