[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

reopening classes in modules ?

konsu

1/27/2006 6:55:00 PM

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

1 Answer

George Ogata

1/28/2006 4:22:00 AM

0

"ako..." <akonsu@gmail.com> writes:

> 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