On Sat, 2 Sep 2006, Patrick Gundlach wrote:

> Hello,
>
> another 'best practice' question. Example: I have a library which I
> think should go into
>
> lib/foo/bar/baz.rb

[snip]

> I'd like to be a good ruby-citizen. My personal preference though would
> be something different:
>
> module Baz
>  class SomethingElse
>  end
> end
>
> so the scope would be
>
> s=Baz::SomethingElse.new

How you scope your class has a subjective element.  Only you can decide 
what the best organization of your class hierarchy is, for your uses.

However, in the general case, yes, definitely put your class under at 
least one level of scoping, such as you illustrate above, especially if 
you plan to release your library for others to use.  Namespace collisions 
are no fun.

Just by way of example, I generally organize classes under a module 
namespace that reflects the name of the project.  Then, under that, I will 
organize similar classes under further names.

So:

Iowa::KeyValueCoding
Iowa::Caches::LRUCache
Iowa::Pools::DBConnectionPool

etc...

I have even done this with certain Ruby class extensions.

So, for example, I have added some things to String and Hash, but don't 
want to either force my users to use my extensions, or interfere with my 
users ability to use their own extensions.  So, any place where I am going 
to need one of those extensions, I work with Iowa::String or Iowa::Hash 
instead of String and Hash.  It's a little more typing for me, but better 
for my users.


Kirk Haines