syntax - How does the :: operator work in Ruby? -
i new ruby , confused :: operator. why following code output 2, 3, 4, 5, 1 , not output 1? thanks!
class c = 5 module m = 4 module n = 3 class d = 2 def show_a = 1 puts end puts end puts end puts end puts end d = c::m::n::d.new d.show_a
if remove last line, see 5, 4, 3, 2. reason body of classes , modules regular code (unlike in other languages). therefore, print statements executed when classes/modules getting parsed.
as how :: works - lets move around scopes. ::a reference a in main scope. a refer a in current scope. a::b refer b, inside a, inside current scope.
Comments
Post a Comment