Class C @ const1 = 1 const2 = 2 @ var3 = 3 def self. m p @ const1, const2, @ var3; end def m2 P @ const1, const2, @ var3; C: M # OK # M # error: undefined local variable or method 'M' end endp C: const2; #2 # When the double colon (: :) method is used in Ruby to call a method or variable, # Ruby tends to use names starting with uppercase letters as constants and names starting with lowercase letters as methods. # Use periods (.) always Used as a method when calling # p c: var3; # error: Undefined method 'var3' for C: Class (nomethoderror) '# p c. var3; # error: Undefined method 'var3' for C: Class (nomethoderror) '# p c: const1; # error: uninitialized constant C: const1 (nameerror) # p c. const1; # error: Undefined method 'st1' for C: Class (nomethoderror) 'C. M #1 #2 # 3C = C. NEWC. m2 #1 #2 #3 #1 #2 #3
In Ruby, class variables can be implicitly used in the instance method, but the usage of class methods must be explicitly used.