Begin forwarded message:
On Jan 14, 2008, at 9:26 PM, James G. wrote:
focus on, rather than inane training book examples (they try hard,
they really do).
Thanks for sending in a solution!
I was hoping to get some feedback, as this is one of my first Ruby
programs (not copied from a book or website).
I’ve looked through the code a bit and have a suggestion: see if you
can “DRY” the code up in places. DRY stands for Don’t Repeat Yourself
and it reminds us that if we’re typing something over and over again,
there’s a better way.
A simple example would be the Scite workaround you are using to print
content immediately. You have a lot of lines like:
puts …
STDOUT.flush
If we move that code into a method:
def force_puts(*args)
puts(*args)
$stdout.flush
end
we can use that from now on.
In this case though, Ruby has a variable to force flushing the
output. If you set it at the top of your script, you can omit all of
the calls to flush():
$| = true
The biggest DRYing opportunity are the sections:
if card_display == 1
# …
end
…
if card_display == 2
# …
end
…
if card_display == 3
# …
end
If you can condense those you can probably remove about 150 lines of
code. That makes it easier to read and maintain, so it’s a big win.
Well, I hope that gives you some fresh ideas.
James Edward G. II
On Jan 15, 8:58 am, James G. [email protected] wrote:
In this case though, Ruby has a variable to force flushing the
output. If you set it at the top of your script, you can omit all of
the calls to flush():$| = true
I personally tend to avoid using those cryptic Perl-like two character
globals. I would write the above as:
$stdout.sync = true
Incidentally, I can’t find any reference to $| in the Pickake, second
edition, so I wouldn’t have even known about it if you hadn’t posted
this. It doesn’t seem to be listed anywhere on pages 334-337, nor in
the index.