I’m puzzling through the nuances of Ruby’s module inclusion mechanism,
both inside and outside of Rails.
Forgive the long text, but the behavior is truly strange and I don’t
know what to make of it.
I wrote some software that uses a 3rd party library (midilib) to convert
files from one format to another. I wrote maybe ten classes for my
program, which I kept in a single directory, but I did not define a
module for my classes. Everything works fine when I invoke my program
from command line, so next I wanted to put this conversion utility
online with Rails.
Putting this directory in /lib under rails and adding my dir to the
rails load paths worked fine and I’m able to deliver the converted
results via Rails.
Next I added a few user authentication methods and put them inside a
module and included this module in my Applications controller. This
worked fine but caused my conversion utility to stop working. Rails did
not seem able to find the classes referenced anymore. So I bundled my
classes into a module, and this fixed Rails, but now it no longer works
on the command line – it complains that one of the classes in the 3rd
party library I use is calling a one of its own private methods. But my
code still works fine from Rails. And even at the command line, it
mostly works because my checkpoint print statements are reached, but it
just chokes right at the end. It’s the exact same code in both cases.
So my manipulating my module boundaries it seems I can make my code
either work from Rails or from the command line, but not both. Any idea
what’s going on?
Is there any scenario in which a module definition around the calling
class might affect the behavior or visibility of a private method in the
included class? I’ve literally spent days experimenting and trying to
understand this, but am not any closer to an answer. My only conclusion
so far is that Ruby’s scoping mechanisms are complex.
Any insights would be much appreciated. Thanks in advance!