3
\$\begingroup\$

I come from the world of Python, so I'm used to every file representing their own enclosed module, never polluting the global environment. Up until now (I've worked with Ruby for a week) I've created one file per class in a nested structure such as this:

lib
`- project_name
   |- sub_module
   |  `- foo_bar.rb
   `- some_class.rb

Where some_class.rb contains:

module ProjectName
    class SomeClass
        # ...
    end
end

And foo_bar.rb contains:

module ProjectName::SubModule
    class FooBar
        # ...
    end
end

This has incidentally worked fine up until now, but I realized that if I require foo_bar.rb before I require some_class.rb I would get a NameError: uninitialized constant ProjectName. So now I'm exploring workarounds for foo_bar.rb, all pretty ugly in my opinion:

module ProjectName
    module SubModule
        # Double indent for all the code in the file!?
    end
end

module ProjectName end
module ProjectName::SubModule
    # Fair enough, I suppose...
end

module ProjectName module SubModule
    # Ugly
end end

I suppose any of these would work and the choice would be a matter of taste, but I'd rather use the option that most developers in this situation uses. The question is in the title, please comment.

\$\endgroup\$

closed as off-topic by Jamal Nov 8 '16 at 21:52

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ In ruby, it is standard practice to indent two spaces (not four). So a double indent is just four spaces, if that helps you cope with it. \$\endgroup\$ – Mark Thomas Jun 4 '13 at 22:45
  • \$\begingroup\$ @MarkThomas, this practice appeared just because some random noob from github adviced it, while others were more accurate and weren't thinking they should dictate any standart. So I will never see any sense of working under the practice born from some random githuber, until some huge comittee make oficially a deep research of how to indent code in Ruby. \$\endgroup\$ – Nakilon Jun 5 '13 at 5:48
  • \$\begingroup\$ @Nakilon the practice is widespread in the Ruby community, and is the defacto standard. For example, the entire Rails codebase uses 2-space indents. \$\endgroup\$ – Mark Thomas Jun 5 '13 at 18:08
  • 1
    \$\begingroup\$ @MarkThomas, Ruby != Rails \$\endgroup\$ – Nakilon Jun 6 '13 at 8:02
  • 2
    \$\begingroup\$ @Nakilon True, but I didn't claim it did. That was an example I'm familiar with. Want others? Let's peruse GitHub for popular Non-Rails Ruby projects. OpsCode Chef: 2 spaces; PuppetLabs Puppet: 2 spaces; Sinatra: 2 spaces; Cucumber: 2 spaces; Watir: 2 spaces. \$\endgroup\$ – Mark Thomas Jun 6 '13 at 12:36
2
\$\begingroup\$

I use the version with double indent and I think it is the common usage:

module ProjectName
    module SubModule
        # Double indent for all the code in the file!?
    end
end

If you don't like the double indention, just don't do it.

It is best practice to indent each code level (module, class, def, loops...), but no must. In Python it is part of the syntax, but not in Ruby.

See also https://stackoverflow.com/questions/3833641/is-there-an-acceptable-way-of-putting-multiple-module-declarations-on-the-same-l

\$\endgroup\$
  • \$\begingroup\$ This seems to me to be the most "correct" method. I've also started using two spaces for indenting to reduce the impact of all the nesting. \$\endgroup\$ – Hubro Jun 7 '13 at 7:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.