Should one use dashes or underscores when naming a gem with more than one word?
Eric Hodel has a blog post on this: A Project Naming Recommendation
Rails solidified the convention of
mapping CamelCase class names to
underscored file names (class
IMAPProcesor is defined in
imap_processor.rb). Using underscored
gem names makes it easy for people to
figure out what file to require (same
as the project name) or what class
name to look for in ri.If I have a plugin gem or an extension
I’ll tack on the sub-project’s name
with a dash. If I wanted to add a new
handler for imap_to_rss for Chase bank
email, the gem would be named
imap_to_rss-chase.
Ruby gem naming conventions
The dashed version is for extensions on other frameworks, like rspec-rails
and the underscore is for part of the normal gem name and should be camelcased in your classes.
So if you have a gem named foo_bar
, the class/module should be named FooBar
. If that gem should have a rails extension which ships as a different gem, it should be called foo_bar-rails
and the module should be called FooBar::Rails
and it should be required as require "foo_bar/rails"
This convention is also what Bundler tries to require.
Admittedly, this convention is not always followed. jquery_rails
should actually be jquery-rails
and factory_girl_rails
should be called factory_girl-rails
. But hey, not everything is perfect.
RubyGems convention docs:
- Naming gems
- Naming patterns
- Make your own gem
Any notes or rules for naming ruby gems with '-' or '_'?
There are clearly examples of gems that don't follow any convention. The convention I have come to like the best is using -
to denote a namespace (::
) boundary and _
as a word separator within a class name.
Examples:
| Main Class | Gem Name | require |
|-------------------|-------------------|-------------------|
| Redis | redis | redis |
| Redis::Namespace | redis-namespace | redis/namespace |
| Redis::NativeHash | redis-native_hash | redis/native_hash |
Standard File Naming Conventions in Ruby
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.
Should I give up grammatical correctness when naming my functions to offer regularity?
The way I see it, a variable is a single entity. Maybe that entity is an aggregate of other entities, such as an array or a collection, in which case it would make sense to give it a plural name e.g. a set of Shape
objects could be called shapes
. Even so, it is still a single object. Looking at it that way, it is grammatically acceptable to refer to it as singular. After all, is_shapes_initialized
actually means "Is the variable 'shapes' initialized?"
It's the same reason we say "The Bahamas is" or "The Netherlands is", because we are referring to the singular country, not whatever plural entity it is comprised of. So yes, is_shapes_initialized
can be considered grammatically correct.
URLs: Dash vs. Underscore
This is just a guess, but it seems they picked the one that people most probably wouldn't use in a name. This way you can have a name that includes a hyphenated word, and still use the underbar as a word delimiter, e.g. UseTwo-wayLinks could be converted to use_two-way_links.
In your example, /about-us would be a directory named the hyphenated word "about-us" (if such a word existed, and /about_us would be a directory named the two-word phrase "about us" converted to a single string of non-white characters.
When should one refer to objects by function/relation (instead of by class) in MVC?
My convention is to use always the function/relation name. I don't understand why do you say it's easier to remember and refactor with the class name. I personally find that everything is easier when you use the function/relation name. It's also more "flexible" because if tomorrow you decide to allow groups to be inside other groups, then member still has sense, yet users isn't quite that right... Users or the class name is the "current" implementation/solution of what you are using... The functional or relation name is how that object will be used... It's more related to the "interface" or what you expect it to have... So you are focusing on what thing it should do, and what it should solve, and not in how it does it ATM.
It's also generally easy to deduce the type or possible types of something by looking at the context, and or/running tests, so I think that the functional relational name adds a lot of useful information, while the other adds useless information, that might be more adversary than good.
Hyphenated subcommand in Thor
There's no map
required, just
class Test < Thor
desc 'howto-dash', "dash in command name"
def howto_dash
puts "dashing through the snow"
end
end
Output:
> thor list
test
----
thor test:howto-dash # dash in command name
Related Topics
How to Include Variables in My Vagrantfile
Rails. How to Store Time of Day (For Schedule)
Is There a "Main" Method in Ruby Like in C
What Is the Modern Way to Structure a Ruby Gem
Rails 3:How to Generate Models for Existing Database Tables
Activerecord Select Except Columns
Safely Assign Value to Nested Hash Using Hash#Dig or Lonely Operator(&.)
How to Break from Nested Loops in Ruby
Converting Utf8 to Ansi with Ruby
Getting Only New Mail from an Imap Server
Where in the Ruby Language Is %Q, %W, etc., Defined
What Does Post.All.Map(&:Id) Mean
Iconv Deprecation Warning with Ruby 1.9.3
Rails Migration Changing Column to Use Postgres Arrays
How to Use Watir::Waiter::Wait_Until to Force Chrome to Wait