Seeking feedback on my first Ruby program

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?

http://www.joecheng.com/code/erb-site.rb.txt

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.

For example, if this is your directory tree:

    source/
      functions.rb           <= ignored
      index.htm.!!           <= processed
      guestbook.php          <= copied
      contactme.!!.php       <= processed
      images/
        banner.gif           <= copied
      private/
        photos.!!.htm        <= processed

And you ran this command:

    erb-site.rb source build

Your build directory would end up like this:

    build/
      index.htm
      guestbook.php
      contactme.php
      images/
        banner.gif
      private/
        photos.htm

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.

Guy Decoux

How do I get access to what’s in $stderr in the form of a string?

In particular, I am using the following code

require 'shell’
Shell.def_system_command(“dialog”)
shell = Shell.new
shell.transact do
shell.dialog("–title", “test”, “–menu”, “test”, “0”, “0”, “9”, “test
tag”, “test item”)
end

dialog directs output, in this case, “test tag” to STDERR after the
user presses enter.

I would like to capture the value “test tag” as string assigned to a
variable.

Does anyone know a way to do this? Hopefully a simple way.

I’m aware of a way to do something similar provided by Brain Candler in
an earlier message:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/71151

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.

Thank you in advance for any help.

Regards,

Mark Wilson

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?

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:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/71151

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]

Where can I find documentation for these modules?

You have some documentation in the pickaxe

   http://www.rubycentral.com/book/

Chapter "Standard Library"

Or do I have to just read the code?

you can also : it's easier to read the code than read "anglois" :slight_smile:

Guy Decoux

http://www.rubycentral.com is a good place to start. It has the
documentation for most of the standard modules.

In article TiiNa.25616$C83.2368021@newsread1.prod.itd.earthlink.net,

···

Joe Cheng code@joecheng.com wrote:

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.

Hope this helps,

Mike


mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

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:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/71151

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.

There is a straightforward version here:
http://ruby-talk/72502

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:

Typo, I meant
http://ruby-talk.org/72502

···

On Sun, Jul 06, 2003 at 05:26:25PM +0900, Brian Candler wrote:

There is a straightforward version here:
http://ruby-talk/72502

Thank you. This is excellent stuff.

[snip]

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: