convention for deep classes/modules

D

David Garamond

Is the more common convention:

module Foo
module Bar
class Baz
...
end
end
end

or this:

module Foo; end
module Foo::Bar; end
class Foo::Bar::Baz
...
end

I prefer the second because I dislike having to indent several times.

Also, is there a possibility that Ruby will "autovivify":

module Foo::Bar::Baz
class Foo::Bar::Baz2

so it creates modules Foo, Bar, Baz automatically?
 
H

Hal Fulton

David said:
Is the more common convention:

I prefer the second because I dislike having to indent several times.

This is possible now but wasn't always. So it may be that older code
didn't do it because it couldn't.
Also, is there a possibility that Ruby will "autovivify":

module Foo::Bar::Baz
class Foo::Bar::Baz2

so it creates modules Foo, Bar, Baz automatically?

I don't think this is possible, because it needs to know at each
step whether the new thing is a class or a module.


Hal
 
D

David A. Black

Hi --

Is the more common convention:

module Foo
module Bar
class Baz
...
end
end
end

or this:

module Foo; end
module Foo::Bar; end
class Foo::Bar::Baz
...
end

I prefer the second because I dislike having to indent several times.

Keep in mind that they're not interchangeable:

module A
X = 1
class B
puts X # A::X is visible here
end
end

module C
X = 1
end

class C::B
puts X # C::X is not visible here
end

Also, is there a possibility that Ruby will "autovivify":

module Foo::Bar::Baz
class Foo::Bar::Baz2

so it creates modules Foo, Bar, Baz automatically?

No, because you can't tell whether the lefthand ones (Foo and Bar in
your example) are modules or classes.


David
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top