reopening classes in modules ?

A

ako...

hello,

module M
class C
def c() puts 'c' end
end
end

module M
class C
def c() puts 'cc' end
end
end

M::C.new.c

produces the same output as

module M
class C
def c() puts 'c' end
end
end

class M::C
def c() puts 'cc' end
end

M::C.new.c


does anyone know what the difference is between reopening a module and
then reopening a class in it as in the first code fragment and just
reopening a class defined in a module as in the second fragment?

as far as i can tell there is some difference because my real code
which is more complicated than the above works in case when i reopen
M::C and does not work in case when i reopen M and then reopen C.

thanks
konstantin
 
G

George Ogata

ako... said:
does anyone know what the difference is between reopening a module and
then reopening a class in it as in the first code fragment and just
reopening a class defined in a module as in the second fragment?

Scoping?:

class X
end

module M
class X
end

class C
def foo
puts X.name
end
end
end


class M::C
def bar
puts X.name
end
end

mc = M::C.new
mc.foo # => M::X
mc.bar # => X
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top