Mark Hubbart
12/2/2004 7:57:00 AM
Hi,
On Thu, 2 Dec 2004 15:17:43 +0900, Martin Kahlert <mkcon@gmx.de> wrote:
> Hi!
>
> I want to print out the 'printable' characters from a binary
> data stream.
> Is there anything like C's isprint() function in Ruby?
Not that I know of. But here's a way to get a copy of a string with
all non-printable characters stripped out:
# get a string with all possible byte values:
str = (0..255).map{|b| b.chr}.join
# print all printable characters:
print str.gsub(/[[:^print:]]+/, "")
That gsub there finds all runs of non-printing characters, and
replaces them with an empty string (ie, nothing).
HTH,
Mark