From: mss Date: 2005-01-21T01:21:00+09:00 Subject: Yet another gsub question. Greetings All, Yes, Ruby newbie here. I started reading up on Ruby a week or so ago and have dug up quite a few web sites, docs, etc. Anyway, after spending the past week reading, and at least 6 hours (if not more) Google-ing and looking up gsub examples, I am still at a loss. I see lots of examples of substituting \ with \\, etc. I have yet to see an example of replacing \" with a simple ". Anyway, I've Googled 'til I'm exhausted and this is my last ditch effort to learning Ruby. I could have written this script in PERL 10 times over, in the time it has taken me to get no-where with Ruby. Anyway, the following code should pull the HTML from a web site and present it in such a manner as to allow redirection to a file in a good HTML format. ---8<--- # Example from # http://phrogz.net/ProgrammingRuby/lib_network.html#networkandweblibraries require 'net/http' # Attempt to make a connection. h = Net::HTTP.new(ARGV[0], 80) # Attempt to get the page / data. resp, data = h.get('/', nil ) # Display the status and messages. puts "Code = #{resp.code}" puts "Message = #{resp.message}" resp.each {|key, val| printf "%-14s = %-40.40s\n", key, val } # Actual web page contents data.gsub!(/\"/,'"') p data ---8<--- >From the command line, I issue the following command: myunixbox > ./rover.rb www.somesite.com The output then resembles: ....
... (note the \") If I attempt to redirect the output to a file: myunixbox > ./rover.rb www.somesite.com > somefile.html The \" also get stuck in the file. However, if I change the gsub code to: data.gsub!(/\"/,'*') The output changes to: ....
... Obviously gsub is properly detecting the \" pair, and it knows to replace it with * (in that example), but for the life of me I cannot figure out why it has to keep escaping the ". Any suggestions would be greatly appreciated. Thank you, -Matt