Ruby is a convenient language, but there are jambs.

I am concerned that modules connect like a #include . It is not completely clear how to resolve name conflicts.

If you connect two files with the same names of methods / classes, then they are simply triturated last. Although it is convenient, but with the included files is better to lead otherwise.

There is a module keyword, similar to a namespace. And with it you can escape from rubbing. But I conducted an experiment: if you declare modules, then when modules with the same name are connected, silent rubbing of functions and classes with the same name occurs.

Of course, there is such an option: all the distributed code should be contained in a module with a unique name.

Actually a question. How is it common to solve such problems in Ruby? What else do I need to know about the Ruby modular system?

    2 answers 2

    It's not a bug, it's a feature. Tsimes Rubin in a similar openness among other things. If you are worried (generally speaking, this is not a problem before, say, a certain level of language use), and you want to shake hands, look at https://github.com/banister/remix and https://github.com/banister / prepend

      The answer is very late, yes, but the answer about why this is not a problem is still not!


      In the Ruby ecosystem, conventions are particularly strong. When a language imposes so little, the rules are generated by the user community itself. And who does not comply with them, the community can strongly dislike those :)

      On RubyGems (in the main source of libraries) there are recommendations on naming , following which, such a conflict can only be obtained on purpose. How? It is very simple: there are conventions on how, based on the name of a heme, to figure out how the main module of the heme should be called and in which file / along which path it should be placed.

      Ruby does not enforce these rules (unlike Java, for example). But observing them is easier to live in the language ecosystem.

      Exhaustive example:

      • If called gem net-http-digest_auth
      • ... then its main module is Net::Http::DigestAuth (which should contain most of the heme)
      • ... and it loads via require 'net/http/digest_auth'
        • Most likely, in geme he will be in lib/net/http/digest_auth.rb

      That is, the assumption that RubyGems will be used as the main source of libraries, and conflicts between libraries can be minimized simply by looking at the list of gems from there.

      And you know? Practice shows that people are looking there . That is, it works.


      This is all true, a little breaks when it comes to plugins . That is, gems that extend the functionality of other gems. Sometimes conflicts occur when several plugins are loaded to a single heme, since they all somehow influence it.

      The risk of conflict is especially great if the extensible heme has no API for extensions. In this case, dirty tricks like monkeypatch come into play, which can end with anything.