From: Francis Cianfrocca Date: 2007-05-26T21:14:21+09:00 Subject: Re: Forward references? ------=_Part_37376_23372853.1180181660861 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 5/26/07, Peter Seebach wrote: > > > class A::B > end > class A > end > --> t.rb:1: uninitialized constant A (NameError) > > So I can't just do them in ANY order. In this construction you're defining B inside the naming context of A, which doesn't exist when the *parser* (which obeys Ruby scoping rules like any other Ruby program) needs to use the value of A. That's why I suggested something like this: class A class B def bar end end end class A def initialize @b = B.new end end You see the difference? You don't have to define class A before you define class B, but you DO need to *declare* A before you can present it to Ruby's parser in a construction like A::xxx. Of course your original question related to style rather than parser behavior. You don't say whether you come from a Java background, where the relationship between classes and source files is much stricter than it is in Ruby. With closely related Ruby classes like yours, I often define them in separate text blocks, in the same file: module MyApplication class A def initialize @b = B.new end end end #------------------------------ module MyApplication class A class B end end end ------=_Part_37376_23372853.1180181660861--