Ruby Naming Conventions

Jeffrey Hardy,
Oct 19, 2007

From the Softies on Rails comes a quick summary of idiomatic Ruby naming conventions. To a seasoned Rubyist, these are common knowledge, but I’ve seen many a beginner carrying their baggage from other languages over to Ruby.

Ruby 101: Naming Conventions

In the book, I wrote the following sidebar that covers the basics (from page 330):

Ruby Style

Style is important when programming. Ruby programmers tend to be picky about style, and generally adhere to a few specific guidelines:

  • Indentation size is two spaces
  • Spaces are preferred to tabs
  • Variables should be lowercase and underscored: some_variable, not someVariable or somevariable
  • Method definitions should include parentheses and no unnecessary spaces: MyClass.my_method(my_arg), not my_method( my_arg ) or my_method my_arg

Whatever your personal style, the most important thing is that you remain consistent. There’s nothing worse than looking at code that switches between tabs and spaces, or mixed and lowercase variable names.

Comments

Leave a response