Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the preferred way (better style) to name a namespace in Ruby? Singular or Plural?

What are for you the pros and cons of using:

FooLib::Plugins FooLib::Plugins::Bar 

vs.

FooLib::Plugin FooLib::Plugin::Bar 

naming conventions? And what would you use or what are you using? What is more commonly used in the comunity?

like image 470
Szymon Jeż Avatar asked Feb 15 '12 12:02

Szymon Jeż


People also ask

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 is namespace in Ruby?

Namespace in Ruby allows multiple structures to be written using hierarchical manner. Thus, one can reuse the names within the single main namespace. The namespace in Ruby is defined by prefixing the keyword module in front of the namespace name. The name of namespaces and classes always start from a capital letter.

How do I use a module in Ruby?

The method definitions look similar, too: Module methods are defined just like class methods. As with class methods, you call a module method by preceding its name with the module's name and a period, and you reference a constant using the module name and two colons.


1 Answers

Use:

module FooLib end module FooLib::Plugins end class  FooLib::Plugins::Plugin; end #the base for plugins class  FooLib::Plugins::Bar < FooLib::Plugins::Plugin; end class  FooLib::Plugins::Bar2 < FooLib::Plugins::Plugin; end 

or in a different words:

module FooLib   module Plugins     class Plugin; end #the base for plugins     class Bar < Plugin; end     class Bar2 < Plugin; end   end end 

Also arrange the files like this:

- foo_lib/   - plugins/     - plugin.rb     - bar.rb     - bar2.rb 

This is how Rails does it (so this is the Rails Way). I.e. look at the Associations namespace and the Associations::Association class from which all of the classes form the Associations namespace inherits (i.e. Associations::SingularAssociation).

like image 124
Szymon Jeż Avatar answered Sep 21 '22 21:09

Szymon Jeż