Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - Naming Convention - letter case for acronyms in class/module names?

I need to create a class that represent "SVN" inside a module called "SCM". But I don't know what is the convention when dealing with acronyms in Ruby, and could not find anything relevant in Google, except "Camel case is preferred".

Should I call it SCM::SVN or Scm::Svn? Is there a convention for this?

like image 424
kikito Avatar asked Feb 15 '12 12:02

kikito


People also ask

How do you name a module in Ruby?

Classes and modules use MixedCase and have no underscores, each word starts with a uppercase letter. Database Table - e.g. invoice_items, orders Table names have all lowercase letters and underscores between words, also all table names need to be plural.

What's the proper convention for naming variables in Ruby?

Variable names in Ruby can be created from alphanumeric characters and the underscore _ character. A variable cannot begin with a number. This makes it easier for the interpreter to distinguish a literal number from a variable. Variable names cannot begin with a capital letter.

What are naming conventions examples?

What is an example of a good naming convention? Good naming examples include: [Project number] - Data Use Agreement - [Title of research project] Approval - Change to employee travel policy - February 2014.

Should variables be CamelCase?

Methods should be verbs in lowerCamelCase or a multi-word name that begins with a verb in lowercase; that is, with the first letter lowercase and the first letters of subsequent words in uppercase. Local variables, instance variables, and class variables are also written in lowerCamelCase .


2 Answers

Add the following to config/initializers/inflections.rb.

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'SVN'
end

Now running $ rails g model SVN… will create a class named SVN in a file named svn.rb and an associated table svns.

like image 191
Mike Avatar answered Sep 29 '22 17:09

Mike


SCM::SVN looks best to me. Rails is full of classes like ERB, ORM and OMFGIMATEAPOT. And that's not to mention things like JSONSerializer. Ruby's source has a bunch of acronyms, too. The most obvious example to me is YAML. The standard as I've seen it is to upcase letters for CamelCase but generally not to downcase them (although Rails has opinions on model names).

If you have grep and the source code you can see plenty of examples with something like

grep -r 'class [A-Z]\{3,\}' <path/to/source>
# or, if you only want acronyms and nothing like YAMLColumn:
grep -rw 'class [A-Z]\{3,\}' <path/to/source>
like image 36
brymck Avatar answered Sep 29 '22 18:09

brymck