Yet another gsub question.

M

mss

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:
.... <div class=\"footer\"> ...

(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:
.... <div class=*footer*> ...

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
 
J

James Edward Gray II

data.gsub!(/\"/,'"')

\ has special meaning in a regex and thus, needs escaping (just like it
would in Perl):

data.gsub(/\\"/, '"')

Hope that helps.

James Edward Gray II
 
T

ts

m> data.gsub!(/\"/,'"')
m> p data

This is not a problem with #gsub but a problem with #inspect

uln% ruby
data = 'a quote here ==> "'
p data
puts data
^D
"a quote here ==> \""
a quote here ==> "
uln%

See that #inspect add a \ before "

use puts



Guy Decoux
 
M

mss

Changed:
# data.gsub!(/\"/,'*')
data.gsub(/\\"/, '"')

....as suggested.

Nada. Output is still:
.... <div class=\"footer\"> ...

(note the \")

Does the same thing as before. Honestly, I can't tell you how many
combinations I've tried within gsub. Thus my cries and pleas for help
here =/.
 
M

mss

p data
puts data

!!!! BINGO !!!! - changed the p to puts and it turned out perfect.
Thank you both for the suggestions and help.
 
J

James Edward Gray II

Changed:
# data.gsub!(/\"/,'*')
data.gsub(/\\"/, '"')

....as suggested.

Nada. Output is still:
.... <div class=\"footer\"> ...

(note the \")

Yes, that was me being sloppy. I didn't notice your use of p() to
print, as Guy did. Usually, you want puts() or print() for output.
p() is primarily a debugging tool.

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top