From: David Alan Black Date: 2001-12-17T02:43:24+09:00 Subject: [ruby-talk:28699] Re: How do you do "character filtering" of a string using each_byte. II Hello -- On Mon, 17 Dec 2001, Thaddeus L. Olczyk wrote: > There has been a trend I noticed in the last few years of the Usenet. > Basically it has been the dumbing down of Usenet. It used to be that It's nothing new. Personally, I've been using Usenet since 1990 -- and since, on your construction, I'm part of the problem, rather than the solution, that's at least 11 years of uninterrupted mediocrity. > In any case I now present a version of the problem where the basics > almost have to be handled. > > Given a string I want a function to produce a string that replaces the > characters by the following rule. > 1) If the character is lower-case, I want it replaced by \x followed > by the hex value. > For the truely intelligent who try this, thanks. I know I'm not among the "truely" (or perhaps even truly) intelligent, so I'll limit myself to just this one requirement. (You have to be careful with people like me. Just as mortals cannot bear seeing Zeus decked out in his full glory, we sub-intelligent critters tend to get knocked senseless by too much brilliance emanating from any one source. As you are strong, be merciful.) I take it that you MUST use String#each_byte, so here: str = "hello 123 I am a String" str2 = "" str.each_byte {|b| str2 += case b when ?a..?z "\\x#{sprintf('%x',b)}" # shorter way to do this? else b.chr end } After you've finished this, you might want to contact: 1. Yale University -- tell them to revoke my BA. 2. New York University -- they slipped up and gave me a Ph.D. 3. University of Illinois Press -- I hoodwinked them into publishing my first book. 4. Seton Hall University -- biggest suckers of all: they gave me tenure. Love, David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav