I just took my first stab at writing a useful Ruby program. My programming
background is mainly Java and C#, so I wonder if I’m really doing things the
"Ruby Way". Would someone mind taking a look at my code and giving me
feedback?
It’s a command-line utility for making static websites using erb. It just
recurses over a source directory, processing and copying files to a build
directory. I’m including the full description below.
Any feedback is appreciated… thanks…
···
Usage: erb-site.rb [options]
-q, --quiet Suppress all messages but warnings and errors
-v, --verbose Enable verbose logging
-d, --debug Enable debug logging
-wval, --watch val Change watch character to val (default is !!)
-h, --help Detailed help
NOTE: erb must be installed (tested with erb-2.0.3).
erb-site can be used to run erb recursively over a
directory that contains both erb and non-erb files.
This is useful for creating static sites, for example.
Use erb-site to process a directory that contains
eRuby files (and other files)–your “source” directory.
You also need to provide a “build” directory path (it
doesn’t need to exist, and in fact if it does exist,
it will be recursively DELETED before processing begins).
erb-site will crawl your source directory, and for each
file it encounters:
with .!! extension (or with “.!!.” in the filename)
Process using erb, and save the results to the
corresponding path in the build directory minus
the .!! extension (or minus the “.!!.” in the
filename). You can use your own string in place
of “!!” by specifying the --watch option.
(Note that file.!!.txt, file.txt.!!, and file.!!
all match, but file!!.txt and file!! do not.)
with .rb extension
Ignore completely. This allows you to keep helper
functions and modules in separate files without
worrying about them getting into your deployment.
all other extensions
Copied to corresponding build directory with no
changes.
It's a command-line utility for making static websites using erb. It just
recurses over a source directory, processing and copying files to a build
directory. I'm including the full description below.
Well, perhaps it's best if you use some module from the standard
distribution. For example, you have 'find.rb'
···
#
# find.rb: the Find module for processing all files under a given directory.
#
#
# The +Find+ module supports the top-down traversal of a set of file paths.
#
#
You have also some module, like 'ftools.rb' or 'fileutils.rb' if you want
to copy or remove files.
I tried Brian’s solution and it worked perfectly. I should have tried
it before asking. I still don’t really follow how it works (it’s a
little like magic to me), so any education on that would be good.
I think that a declarative way of accessing the contents of what a
method writes to stderr would be a good addition to Ruby.
Regards,
Mark Wilson
···
On Friday, July 4, 2003, at 06:51 PM, Mark Wilson wrote:
How do I get access to what’s in $stderr in the form of a string?
[snip]
I’m aware of a way to do something similar provided by Brain Candler
in an earlier message:
Well, perhaps it’s best if you use some module from the standard
distribution. For example, you have ‘find.rb’
Excellent, thank you… I didn’t even know all those modules were hanging
out in the lib directory.
Where can I find documentation for these modules? Or do I have to just read
the code?
You might look at http://www.rubycentral.com/book/index.html (but a
paper copy is a lot easier to use…) The sections after Built-in
Classes and Methods discuss what came with ruby 1.6.
I am having trouble following how I might implement the above, and I
don’t know if it is the answer to the difficulty I am having.
[snip]
I tried Brian’s solution and it worked perfectly. I should have tried
it before asking. I still don’t really follow how it works (it’s a
little like magic to me), so any education on that would be good.
It forks off a subprocess, passes a pipe to the child’s stdout and stderr,
and reads from the other end of the pipe into a string.
It was an attempt to solve a different problem: how to capture stdout/stderr
of any arbitary Ruby command, including one which calls C. It’s inefficent
because of the extra fork (since Kernel#system itself does a fork)
I think that a declarative way of accessing the contents of what a
method writes to stderr would be a good addition to Ruby.
It has been suggested that an alternative version of Kernel#system which
captures stdout and stderr to separate variables would be good to have. You
can currently capture stdout using backtick or %x{}, but not stderr.
However this is an implementation of regular ‘system’, not the
Shell.def_system_command / #transact / #dialog you are using (which I had
never come across before)
Regards,
Brian.
···
On Sun, Jul 06, 2003 at 04:59:16PM +0900, Mark Wilson wrote:
However this is an implementation of regular ‘system’, not the
Shell.def_system_command / #transact / #dialog you are using (which I
had
never come across before)
[snip]
The use of shell.transact is another example of my “use magic” approach
to Ruby programming (at least as it pertains to multiple processes and
inter-process communication). Before using it, I would encounter
synchronization problems when the same object made multiple calls to
system processes. This “technique” may be overkill in many cases, and I
will have to learn more to distinguish when it’s necessary and when
it’s just more overhead.
Regards,
Mark
···
On Sunday, July 6, 2003, at 04:26 AM, Brian Candler wrote: