Forward references?

Discussion in 'Ruby' started by Peter Seebach, May 26, 2007.

  1. I am trying to figure out what the cleanest way is to express a class that has
    subclasses.

    Imagine, if you will:
    class Foo
    def initialize
    @bar = Foo::Bar.new(self)
    end
    end

    class Foo::Bar
    def initialize(foo)
    @foo = foo
    end
    end

    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.

    Of course, I can write:
    class Foo
    end
    require 'foo/bar.rb'

    Is there a clear preference here in Ruby Style?

    -s
    Peter Seebach, May 26, 2007
    #1
    1. Advertising

  2. On 26.05.2007 13:12, Peter Seebach wrote:
    > I am trying to figure out what the cleanest way is to express a class that has
    > subclasses.


    First of all, your example is about nested classes and not subclasses.

    > Imagine, if you will:
    > class Foo
    > def initialize
    > @bar = Foo::Bar.new(self)
    > end
    > end
    >
    > class Foo::Bar
    > def initialize(foo)
    > @foo = foo
    > end
    > end
    >
    > 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
    => nil
    irb(main):002:0> class Foo; end
    => nil
    irb(main):003:0> test
    => #<Foo:0x7ff95f40>

    > Of course, I can write:
    > class Foo
    > end
    > require 'foo/bar.rb'
    >
    > 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.

    I'd do

    class Foo
    def initialize
    @bar = Bar.new self
    end

    class Bar
    def initialize(foo)
    @foo = foo
    end
    end
    end

    irb(main):012:0> Foo.new
    => #<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).

    Kind regards

    robert
    Robert Klemme, May 26, 2007
    #2
    1. Advertising

  3. Peter Seebach

    Jeremy Henty Guest

    On 2007-05-26, Peter Seebach <> wrote:

    > class Foo
    > def initialize
    > @bar = Foo::Bar.new(self)
    > end
    > end
    >
    > [snip]
    >
    > ... If I put Foo::Bar in its own file, should foo.rb be responsible
    > for requiring it?


    I would say yes. My rule of thumb is that any file that explicitly
    calls some API must explicitly require/use/#include/... that API.
    IOW, batteries *should* be included; don't force your users to make
    extra trips to the shops.

    Regards,

    Jeremy Henty
    Jeremy Henty, May 26, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    1
    Views:
    593
    Jonathan Turkanis
    Feb 15, 2004
  2. Boris Du¹ek
    Replies:
    3
    Views:
    371
    Juha Nieminen
    May 2, 2009
  3. Lorenzo Di Gregorio

    Inheritance and forward references (prototypes)

    Lorenzo Di Gregorio, Jun 20, 2009, in forum: Python
    Replies:
    14
    Views:
    471
    Lorenzo Di Gregorio
    Jun 22, 2009
  4. Lloyd Zusman

    Forward references?

    Lloyd Zusman, Jul 30, 2004, in forum: Ruby
    Replies:
    22
    Views:
    470
    Lloyd Zusman
    Aug 3, 2004
  5. mathog
    Replies:
    7
    Views:
    236
    Martin Shobe
    May 31, 2013
Loading...

Share This Page