[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Yet another gsub question.

mss

1/20/2005 4:19:00 PM

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#networkandwe...
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

5 Answers

James Gray

1/20/2005 4:27:00 PM

0

On Jan 20, 2005, at 10:21 AM, mss wrote:

> 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



ts

1/20/2005 4:32:00 PM

0

>>>>> "m" == mss <matt_smith@bellsouth.net> writes:

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


mss

1/20/2005 4:39:00 PM

0

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 =/.

mss

1/20/2005 4:42:00 PM

0

p data
puts data

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

James Gray

1/20/2005 4:49:00 PM

0

On Jan 20, 2005, at 10:41 AM, mss wrote:

> 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