Harold Hausman wrote:
> Hi,
>
> [...] the first code I've shared with you guys:

Hi Harold - and thanks for doing that.

> #It creates one character per pixel (which has obvious implications)

Not obvious to me until I tried it.
That's a fair-sized hunk of duck ;-))

> #But it's 40 lines of pure ruby no lib use binary file up-hackery...

Excellent.
I refactored a bit:

#------------------------------------------------------------------------
Gradient = %w{ D Y 8 S 6 5 J j t c + i ! ; : . }

# http://www.d10.karoo.net/ruby/quiz/50/duck.bmp  (NOTE: 800KB  BMP)
bmp = File.open('duck.bmp', 'rb') { |fi| fi.read }
bmo = bmp[10, 4].unpack('V')[0]             # offset to bitmap data
image_x, image_y = bmp[18, 8].unpack('VV')  # width x / height y (pixels)
by_start = bmo + ((image_y - 1) * (image_x * 3))

File.open('output.txt', 'w') do |fo|
  by_start.step(bmo, -(image_x * 3)) do |by_ptr|
    image_x.times do |x|
      t = 0;  3.times {|n| t += bmp[by_ptr + (x * 3) + n] }
      fo.putc( Gradient[ (t / 3 ) >> 4 ] )
    end
    fo.puts
  end
end
#------------------------------------------------------------------------

There's one change in effect from your original and I suspect
you may have intended it differently ...

  the_file.read(1).unpack('c')[0]

.... gives a signed byte so, when you take the average by adding and
dividing by 3, you can be adding negatives.  Replacing those with ('C')
gives you unsigned bytes and the overall result matches the output from
the code above.

Maybe we'll see a smaller fowl soon :-)


daz