# Fwd: Attempted solution. :)

**URL:** <https://www.ruby-forum.com/t/fwd-attempted-solution/126062>\
**Category:** Ruby\
**Created:** [January 15, 2008, 4:28am UTC](https://www.ruby-forum.com/t/fwd-attempted-solution/126062 "2008-01-15T04:28:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [January 15, 2008, 4:28am UTC](https://www.ruby-forum.com/t/fwd-attempted-solution/126062/1 "2008-01-15T04:28:33Z")

</div>

Begin forwarded message:

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [January 15, 2008, 2:59pm UTC](https://www.ruby-forum.com/t/fwd-attempted-solution/126062/2 "2008-01-15T14:59:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [January 15, 2008, 4:01pm UTC](https://www.ruby-forum.com/t/fwd-attempted-solution/126062/3 "2008-01-15T16:01:29Z")

</div>

On Jan 15, 8:58 am, James G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
