views:

39

answers:

2

I think my brain died, but I thought there was a simple way of using 'Modules' in Ruby to create a namespace for my own classes - or did I dream that ?

What I'm after is the Ruby equivalent to the Java-way of putting my class defs in a package ...

Or do I have to just invent a new name for my objects ???

+3  A: 
module ThreadReader

    class Thread
    ...
    end
end

t=ThreadReader::Thread.new(...
=> #<ThreadReader::Thread:...

Doh...yeah, brain-died - I was trying to use 'Module' (capital 'M'), rather than 'module'...

monojohnny
+1  A: 

Well Modules behave this way as you will be aware.

As I'm sure you're also aware there is a certain danger with regard to understandable and maintainable code in creating classes with the same name as other regularly used classes, so you might want to use a different name even with your changed namespace.

glenatron
You are right I know - but it's so hard to think of meaningful yet short names sometimes :)
monojohnny
I used to have that problem. Since I read _Code Complete_ I found I started using longer but clearer names for things. I type fast enough that a few more characters won't do any harm. And of course ThreadReader::Thread is probably longer than ReadableThread at any rate...
glenatron
Will check out Code Complete at some point - cheers.Actually, the short-meaningful names thing isn't to do with typing speed - its more to do with code-indentation.I take your point about ThreadReader::Thread being just as long though :) (Although a few tactical 'includes' help of course - but then the code suffers from possible problems because of the real 'Thread' object of course...)
monojohnny