"private" modules

M

Matt Bleh

Hi,
I'm writing a ruby library, under a module named "A". Since I provide
classes which share names with standard ruby classes (like Vector), this
way I avoid namespace pollution when requiring my library.
The problem is that I have an inner ("private") module "B", defined
under "A" where I have a bunch of methods defined that these classes
will use (I'm using RubyFFI).

But, since (as a user of my library) I would want to "include A" to
avoid the A:: prefix everywhere, the inner module "B" would be exposed
to the top-level.
I don't think it may be really problematic, but I think it may be a
common issue that others may face.

An example:

module A
# I provide a nice Vector implementation, to replace Ruby's
class Vector
def func
# ...
B::c_library_func(p)
# ...
end
end

module B
extend FFI::Library
attach_function :c_library_func, [ :pointer ], :void
# and continues...
end
end

If I do:
include A
Now B is visible to the user, and it may end-up clashing with something
else.

Is there a guideline for these cases?

Thank you
 
R

Robert Klemme

Hi,
I'm writing a ruby library, under a module named "A". Since I provide
classes which share names with standard ruby classes (like Vector), this
way I avoid namespace pollution when requiring my library.
The problem is that I have an inner ("private") module "B", defined
under "A" where I have a bunch of methods defined that these classes
will use (I'm using RubyFFI).

But, since (as a user of my library) I would want to "include A" to
avoid the A:: prefix everywhere, the inner module "B" would be exposed
to the top-level.
I don't think it may be really problematic, but I think it may be a
common issue that others may face.

An example:

module A
# I provide a nice Vector implementation, to replace Ruby's
class Vector
def func
# ...
B::c_library_func(p)
# ...
end
end

module B
extend FFI::Library
attach_function :c_library_func, [ :pointer ], :void
# and continues...
end
end

If I do:
include A
Now B is visible to the user, and it may end-up clashing with something
else.

Is there a guideline for these cases?

Thank you

You might be able to pull this off with instance or local variables:

module A
@b = Module.new # or b = ...
extend FFI::Library
...
end

Vector.send :include, @b
end

# untested

Kind regards

robert
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top