On 26.05.2007 13:12, Peter S. wrote:
I am trying to figure out what the cleanest way is to express a class that has
First of all, your example is about nested classes and not subclasses.
Is that a good rubyish way to do this? If I put Foo::Bar in
its own file, should foo.rb be responsible for requiring it?
What originally got me thinking was that my habit is to put requires,
includes, and such at the top of the file… But then the
declaration “class Foo::Bar” refers to an undefined constant.
That’s not an issue as long as you evaluate that only after the constant
has been defined:
irb(main):001:0> def test() Foo.new end
irb(main):002:0> class Foo; end
Of course, I can write:
Is there a clear preference here in Ruby Style?
I would not put these in different files as Foo depends on Foo::Bar and
Foo::Bar depends on Foo.
@bar = Bar.new self
@foo = foo
=> #<Foo:0x7ff7883c @bar=#<Foo::Bar:0x7ff78738 @foo=#<Foo:0x7ff7883c
Another question is whether you really want nested classes or maybe
rather classes in the same namespace. Basically I create nested classes
if the nested class only makes sense in the context of the outer class.
The situation is however different than in Java where all this is much
more strict (and thus clear).