nested namespaces: so many spaces

J

jandot

Hi all,

For one of my libraries, I have to create nested namespaces such as
Bio::Graphics::panel::Track::Feature. Code for these is contained in
separated files: the code for Bio::Graphics::panel in panel.rb, the
code for Bio::Graphics::panel::Track in track.rb and
Bio::Graphics::panel::Track::Feature in feature.rb.

Unfortunately, I don't know a way of putting the whole namespace on
one line in e.g. feature.rb. As a result, every single line of code
has to be preceded with 12 spaces. But this can become cumbersome and
is basically a waste of, well, space...

Here's what it looks like:

...module Bio
.....module Graphics
.......class Panel
.........class Track
...........class Feature
.............# all
.............# the
.............# code
.............# needs
.............# 12 spaces
...........end
.........end
.......end
.....end
...end

What I _could_ do is something like this:

...module Bio
...module Graphics
...class Panel
...class Track
...class Feature
.....# all
.....# the
.....# code
.....# just
.....# needs
.....# 4 spaces
...end
...end
...end
...end
...end

But then you end up with just a whole string of "end"s at the bottom
of your file. Isn't there a way to define the complete namespace in
one go on one line?

I've tried things like
...class Bio::Graphics::panel::Track::Feature
.....def initialize()
.....end
...end

But that didn't work. Any suggestions welcome.

Thanks,
jan.
 
F

furtive.clown

Bio = Module.new
Bio::Graphics = Module.new
Bio::Graphics::panel = Class.new
Bio::Graphics::panel::Track = Class.new
Bio::Graphics::panel::Track::Feature = Class.new

class Bio::Graphics::panel::Track::Feature
def foo
puts "foo"
end
end

Bio::Graphics::panel::Track::Feature.new.foo # => "foo"
 
J

jandot

Bio = Module.new
Bio::Graphics = Module.new
Bio::Graphics::panel = Class.new
Bio::Graphics::panel::Track = Class.new
Bio::Graphics::panel::Track::Feature = Class.new

class Bio::Graphics::panel::Track::Feature
def foo
puts "foo"
end
end

Bio::Graphics::panel::Track::Feature.new.foo # => "foo"

Thanks. It looks like this does the trick.

jan.
 
F

furtive.clown

Bio = Module.new
Bio::Graphics = Module.new
Bio::Graphics::panel = Class.new
Bio::Graphics::panel::Track = Class.new

I should clarify that I could have written

module Bio ; end
module Bio::Graphics ; end
class Bio::Graphics::panel ; end
class Bio::Graphics::panel::Track ; end

it's just that I found the former nicer to look at.
 

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

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top