I am attempting to learn ruby and I can not seem to find how I would
structure a ruby application. Not a rails app just a simple ruby app. I
mostly have played around by putting several classes in one file and
running that.
Does one typically put rb files in a package structure like a java
application?
com.jenkins.util.MyClass?
Thanks for your help!
I think the way it often goes, in the simple case, is: you put one
top-level class (or module) per file, and name the file the same name
as the class (or module), but lowercase. Then you dump these files
into your app's lib directory. Remember, from your ruby code, you
"require 'filename'" (without the .rb filename extension) -- you don't
require the class (or module) name.
If you start creating subdirectories in your lib dir, I don't think
the classes therein need to have any relationship to the other
classes. You would simply note the extra directory in your require
statements in your code: "require 'vecmath/matrix'", require
'crystal/tetrahedron'", or whatever. The subdirectories are only there
to help you keep organized, and aren't required.
When you have classes along with subclasses, I'm guessing these
usually just get lumped together into the same lib directory (or
subdirectory under lib). Though, again, they might be separated if you
want to keep them organized in some way, or if they just won't stop
touching eachother and arguing while you're trying to drive the car.
When you've got classes inside other classes (like Java's inner
classes), I'm not sure what to do. Maybe as an example, see your Ruby
installation in .../lib/ruby/1.8/, and note cgi.rb, cgi/session.rb,
cgi/session/pstore.rb. I guess that's the customary way to handle
those... (?)
---John