55

For a file containing the given class, SomeCoolClass, what would be the proper or standard filename?

 1. somecoolclass.rb
 2. some_cool_class.rb
 3. some-cool-class.rb
 4. SomeCoolClass.rb

or some other variation?

I noticed in the Ruby stdlib, versions 1, 2 and 3 are used.

4 Answers4

51

With just Ruby (i.e. not Rails), naming is only a convention. In Rails the convention of using underscores is necessary (almost).

I think convention #2 lowercase_and_underscore.rb is more common and looks pretty good, though an article Here says lowercasenounderscore.rb is the Ruby convention.

Pick either which ever convention is more common or which ever one you like more. The most important thing is to be consistent within a project.

Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62
Daniel Beardsley
  • 19,907
  • 21
  • 66
  • 79
  • 7
    The word from Ruby Gems is to use underscores (a.k.a. box-cutting or snakes) for file names, and Gem names: [Consistent Naming](http://guides.rubygems.org/patterns/#consistent-naming). – stevenharman Aug 03 '11 at 22:25
  • 12
    That article you cited for `lowercasenounderscore.rb` is old and I haven't seen anyone else claiming that as the convention. Underscores is almost universally the standard. – SFEley May 16 '12 at 21:18
9

I personally think the hyphen as word separator makes for maximum readability and typability in general, so I recommend that where possible (in some contexts, a hyphen can't be used, such as in identifiers in most languages). One important thing to bear in mind is that the scheme you pick will have a bearing on the require statement that users will use with your lib, and you want to avoid having a different gem name than library name.

Bad
# gem install my_cool_lib
require 'my-cool-lib'

# gem install MyCoolLib
require 'my_cool_lib'
Good
# gem install my_cool_lib
require 'my_cool_lib'

# gem install my-cool-lib
require 'my-cool-lib'

Unfortunately, a small handful of libraries violate this simple usability rule. Don't be one of those libraries. :)

Pistos
  • 23,070
  • 14
  • 64
  • 77
6

I would recommend lower case characters with underscores (number 2 in your question). It's true that this naming scheme is the convention in Rails and not necessary in non-Rails projects. However, I would still stick to the Rails convention because most Ruby programmers are probably using Ruby exclusively for Rails anyway.

Christoph Schiessl
  • 6,818
  • 4
  • 33
  • 45
  • Not only that, but underscores are the convention for most testing tools, plugin interfaces, etc. as well. (E.g., if you have a file `foo.rb` and a spec file at `spec/foo_spec.rb`, Autotest will run the latter when the former changes, and many text editors will have shortcuts to jump between the two.) – SFEley May 16 '12 at 21:22
5
my-proj
├── README
├── lib
│   └── some_cool_class.rb
└── test
    └── some_cool_class_test.rb
Mike
  • 4,462
  • 4
  • 22
  • 11