modules, a verdict?

Distinguished Ladies and Gents,

I was experiencing some odd behaviour with my app so I decided to make
a simple test to iron it out; I found that it is utterly impossible
for there to be a model in the top namespace by the same name as
another model in another different namespace.

Now, I know modules and namespaces aren't something the Rails Core
puts way up there on the list of priorities, or even if it is on the
list, or any other list, at all, but it would be nice to know if
namespaced models at all is something the Core _wants_ to work in
Rails, or not.

So, if you please.. models in namespaces/models; I know you guys don't
use them much but some of us do, are they Teh Unwanted?

Very best regards,
Tomas Jogin, devoted Rails developer and fan

PS.
My test to prove this problem, if anyone is interested:
1. create a new rails app.
2. generate a model called "Whatever"
3. generate a model called "Other::Whatever"
4. run script/console, type: Whatever and find that you get:
"NameError: uninitialized constant Whatever"
5. type Other::Whatever and find that it works.
6. Delete Other::Whatever, run script/console again, and find that now
the Whatever model magically exists again; it would seem loading
Other::Whatever unloads Whatever, or prevents it from loading, or
something.
7. Please note that Other::Whatever and YetAnother::Whatever plays
nice together, but not with Whatever.

My test to prove this problem, if anyone is interested:
1. create a new rails app.
2. generate a model called "Whatever"
3. generate a model called "Other::Whatever"
4. run script/console, type: Whatever and find that you get:
"NameError: uninitialized constant Whatever"
5. type Other::Whatever and find that it works.
6. Delete Other::Whatever, run script/console again, and find that now
the Whatever model magically exists again; it would seem loading
Other::Whatever unloads Whatever, or prevents it from loading, or
something.
7. Please note that Other::Whatever and YetAnother::Whatever plays
nice together, but not with Whatever.

This is definitely a bug, create a ticket at dev.rubyonrails.org and
perhaps it can be fixed for 1.2, with trunk I see the following:

Whatever

LoadError: Expected
script/../config/../config/../app/models/other/whatever.rb to define
Whatever
        from ./script/../config/../config/../vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:226:in
`load_missing_constant'
        from ./script/../config/../config/../vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:321:in
`const_missing'
        from ./script/../config/../config/../vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:328:in
`const_missing'
        from (irb):1

Which is wrong.

This has been an issue for a long time. My advice would be: Don't Do That or even better Don't Use Modules.

Thijs.

PGP.sig (186 Bytes)

Modules are a very useful tool for organising and namespacing your code. This can be crucial on a large application. Simply saying "don't use modules" because the loading of code in modules is broken isn't really useful advice.

Cheers
Luke

Thanks, but, really, that's not advice at all. Advice would be to
offer a different solution to contextual and/or same-named
identifiers, compartmentalized features, functionality and so forth --
the very problem that the use of namespaces solves.

Furthermore, namespaces, unlike the goto-statement and similar things
you "shouldn't do" in programming, aren't thought of as "harmful"
practices in general. It's just within Rails that namespaces are
frowned upon, and, IMHO, for no good reason (the only semi-logical
reason I can find is: "we can't seem to get 'em right"). Note that I'm
NOT casting any blame here, nor expecting any favors or service; I'm
just rejecting the IMHO baseless proposition to "not use" modules in
favor of, well... in favor of nothing else.

Namespaces aren't evil, not at all. There is absolutely nothing wrong
with using namespaces in general. It's too bad that they just don't
work very well in Rails (but much better now than previously, of
course).

Best regards,
Tomas Jogin

Thanks, ticket created: http://dev.rubyonrails.org/ticket/6031

Hear hear.

Rails itself is heavily namespaced, so it should be wrong to discourage namespaces in user apps. Whatever and Other::Whatever are a great feature of Ruby and shouldn’t kill each other in Rails.

It's just within Rails that namespaces are
frowned upon, and, IMHO, for no good reason (the only semi-logical
reason I can find is: "we can't seem to get 'em right"). Note that I'm
NOT casting any blame here, nor expecting any favors or service; I'm
just rejecting the IMHO baseless proposition to "not use" modules in
favor of, well... in favor of nothing else.

Who says its frowned upon? Michael (core) says that the original report
sounds like a bug and asks for a ticket. Thjis (user/contributor) says
that he has had problems with modules in the past and are now avoiding
them. Neither represent a "Modules Considered Harmful" argument.

The notion that "we can't seem to get 'em right" does bug me, though.
It's another sign of vendoritis and certainly an implicit form of
blame. The simple fact is that most core developers just don't need
modules, so they are not being worked on unless quality bug reports
come in. And even then, they're probably just going to be fixed to the
best of that report.

Instead, it'd be great if people who actually needs/wants modules in
their applications got a "Friends of Modules" group together, pulled up
their sleeves, and started programming to make them solid. This is a
commons and we're eager to see your contributions in the core. I see no
good reason why modules shouldn't work perfectly, except that "the
people who care haven't done the work".

Make it happen! :wink:

I'm not saying anything is evil or harmful at all. I've been bitten by module namespace issues in the past and trust me, it's a pain to debug. I'm just telling you from experience that you might want to consider not using modules if you don't absolutely have to.

That _is_ advice in my opinion.

If you really need modules and you have the time, why not fix the module namespace issue and submit a patch? I'm sure you'll get this accepted in no time.

Kind regards,
Thijs

PGP.sig (186 Bytes)

Just wanted to clarify that I'm certainly not expecting any kind of
vendor-like treatment, by "we" (as in "we can't seem to get 'em
right") I meant the Rails community as a whole, not the core or
anything like that; and further, that this was not an assignment of
blame, but a rather poor excuse to stay away from namespaces. (As if
anyone would avoid use of unicode or i18n just because it's lacking a
bit in the implementation?)

I have gotten the impression that namespaced controllers and models
are kind of frowned upon within the Rails community as a whole, I
guess I projected some of that impression onto Thjis comment.

I'd love to fix the namespace issues myself, but sadly my Ruby-fu
isn't quite up for that kind of challenge. Ruby is a very powerful
language, but that power translates into magic which in turn
translates into confusion. Well, to me it does, at least, and
certainly when it comes to dabbling with other people's code. Guess it
wouldn't hurt to take a look though.

Best regards, and thank you,
Tomas Jogin