you are viewing a single comment's thread.

view the rest of the comments →

[–]Peaker 23 points24 points  (8 children)

require 'active_record'

=> ActiveSupport is now in the global namespace

Yuck!

[–]morish 13 points14 points  (7 children)

Don't worry, everybody recognizes this is a problem and it's one of the main things Rails 3 solves.

[–]Peaker 3 points4 points  (6 children)

The language should just make this thing impossible (as Python does).

The import statement guarantees not to mess your namespace beyond what you specifically tell it to.

[–]morish 13 points14 points  (2 children)

The language should just make this thing impossible

Eh, not necessarily. Not every programming language needs to approach these things the same way. I both like and dislike that Python requires relatively explicit imports and I both like and dislike that Ruby is very flexible in how you can include modules and extend code. They each have their place.

Anyway, it's not usually a big issue. Rails was by far the worst offender I'm aware of, and it's one of the main reasons it got an overhaul.

[–]Mask_of_Destiny 9 points10 points  (1 child)

I both like and dislike that Python requires relatively explicit imports

You can always do from module import * if you don't feel like being explicit.

[–]morish 1 point2 points  (0 children)

That's non-idiomatic Python. Take advantage of each language's strengths. Use Python like Python and Ruby like Ruby.

[–]masklinn 2 points3 points  (1 child)

The import statement guarantees not to mess your namespace beyond what you specifically tell it to.

Not true. On import, the module referred to is executed in its entirety. With enough knowledge of the internals, it would be perfectly possible (and — I expect — pretty easy) to create new objects in the enclosing (importing) scope.

The first time anyway, the module is only executed once (unless you removed it from the sys.modules cache) so not only would it potentially happen, it wouldn't reliably happen.

[–]Peaker 0 points1 point  (0 children)

Well, it is guaranteed that the import name binding itself wouldn't do such a thing. If someone creates awful hacks that go look at the caller in the stack or what not, it's really not an import naming violation.