Ruby - Naming Convention - letter case for acronym

2019-02-11 17:28发布

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?

3条回答
做个烂人
2楼-- · 2019-02-11 18:17

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.

查看更多
劫难
3楼-- · 2019-02-11 18:22

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>
查看更多
一夜七次
4楼-- · 2019-02-11 18:27

I think that SCM::SVN looks better (aesthetically), and I've seen libraries that use the same convention. It's really just a matter of what you think reads better.

(However, note that if you are building a Rails project, and want this module to be autoloaded from the /lib directory, you may have to use Scm::Svn.)

查看更多
登录 后发表回答