From: "David A. Black" Date: 2009-06-21T01:09:28+09:00 Subject: Re: 'move into' a module's namespace in irb Hi -- On Sun, 21 Jun 2009, Max Williams wrote: > aha...i was just playing with this in irb and found something puzzling - > would you mind, while we're on this subject, explaining it to me? > >>> module M; class String; def how_long; "could be anything"; end; end; end > > #here, "hello" and String.new are the same class >>> include M > => Object >>> "hello".class > => String >>> String.new.class > => String > > #here, they're not the same class >>> irb M >>> "hello".class > => String >>> String.new.class > => M::String > > It looks like the core String and the module's version are colliding in > a weird way. When you do irb M, you're putting M before all else in the constant resolution path. So String.new is really M::String.new. "hello" is still a ::String (top-level, core String class), because the existence of M::String does not affect the behavior of the literal quotation marks (which create a ::String). David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Now available: The Well-Grounded Rubyist (http://manning.com/black2) "Ruby 1.9: What You Need To Know" Envycasts with David A. Black http://www.envycasts.com