From: Matthew Kerwin Date: 2013-02-22T18:51:50+09:00 Subject: Re: backslash substitution Mario Ruiz wrote in post #1098399: > I found this solution... but i thought that Ruby had a better way: > > x="xxxxx P jjjjjj\0\1\2\3.4.1\4\5\6\7\8\9" > m="" > x.each_byte {|a| > if a<32 then > m+="/" + a.to_s() > else > m+=a.chr > end > } > puts m If you persist on pushing down this path, indeed Ruby does have a better way. Several. Legion. For example, and off the top of my head: x.gsub(/[\x00-\x1F]/){|c|"\\#{c.ord}"} However, and I'll say this one last time, your test code is FAULTY. It doesn't reflect the case you're trying to test. Have a look really closely at the "\8\9" part, have a look how that's interpreted by Ruby. Use the code snippets I so caringly wrote and demonstrated earlier. And then, try reverse-engineering this string using your method: x="c:\a\b\e\xface" If you want to test your gsub line and make sure it's replacing slashes with other slashes, either try it by putting it inside the actual ARGV.each block, or give it some input that matches what ARGV actually contains. If your ARGV contains rubbish, fix it at the calling context. -- Posted via http://www.ruby-forum.com/.