Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Forward references?

Reply
Thread Tools

Forward references?

 
 
Peter Seebach
Guest
Posts: n/a
 
      05-26-2007
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

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      05-26-2007
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
 
Reply With Quote
 
 
 
 
Jeremy Henty
Guest
Posts: n/a
 
      05-26-2007
On 2007-05-26, Peter Seebach <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thunderbird 0.7 / 0.8 Forward Mark Dalgua Firefox 1 10-22-2004 06:56 AM
How to forward ports... =?Utf-8?B?Y29tZWR5XzE3?= Wireless Networking 1 08-03-2004 11:45 PM
Thunderbird 0.5 doesn't forward attachments + little issue with news carreg3|j'aime pas le spam| Firefox 5 04-04-2004 12:59 PM
Re-forward declaration of types which were already forward declared qazmlp C++ 1 02-15-2004 07:00 PM
Hot to change the forward header...? Marius Firefox 0 08-27-2003 06:42 PM



Advertisments