Deleted unwanted characters from a string


J

Juo Hundred

Hi :)

Im trying to write a script that can check if Rapidshare links are
online, ive managed to get the content of
Code:
 tags from my website
using scrubyt

[code]<root>
<link>http://rapidshare.com/files/example/example.rar</link>

Im trying to strip the string of anything but the
"http://rapidshare.com/files/example/example.rar" part, so I can work
with it.

I tried to use the .delete function however it doesn’t work.

Code:
require 'rubygems'
require 'scrubyt'

data = Scrubyt::Extractor.define do
fetch 'http://www.mywebsite.com/index.php?action=login'
fill_textfield 'user', 'myusername'
fill_textfield 'passwrd', 'mypassword'
submit
fetch 'http://mywebsite/index.php?topic=672'
link '//div/code'
end

rapidshare = data.to_xml.write($stdout, 1)
rapidshare.to_s
rapidshare.delete "<link>"
print rapidshare

Any help would be great appreciated :)

is my full code
 
Ad

Advertisements

M

Matt Neuburg

Juo Hundred said:
rapidshare.delete "<link>"

You are throwing away the new version of the string. Capture it, like
this:

rapidshare = rapidshare.delete(whatever)
 
J

Juo Hundred

Hey, I tried that

Code:
rapidshare = data.to_xml.write($stdout, 1)
rapidshare.to_s
rapidshare = rapidshare.delete(<"link">)
print rapidshare

is now at the end of my code, however I still get the error
"NoMethodError: undefined method ‘delete’ for 139:Fixnum"
 
R

Rick DeNatale

Hey, I tried that

Code:
rapidshare = data.to_xml.write($stdout, 1)[/QUOTE]

This is setting rapidshare to whatever the String#write method which
is an extension to the String class provided by scrubyt.

It looks like it actually returns the number of characters written,
let's say for arguments sake that you write 42 characters, the
actually value doesn't matter since:[QUOTE]
rapidshare.to_s[/QUOTE]
This evaluates to "42" or whatever the string representation of the
return value from String#write was, and then does noting with the
result.

Maybe something like:

rapidshare = data.to_xml
rapidshare.write($stdout, 1)

which would make rapidshare reference the result of data.to_xml (which
I'm assuming here is a string.

Now to the real meat,[QUOTE]
rapidshare = rapidshare.delete(<"link">)[/QUOTE]

As Inigo Montoya would say, "I don't think that method means what you
think it means."

irb(main):001:0> "if l < k and k <  j then j > l".delete("<link>")
=> "f    ad    j the j  "

String#delete removes ANY characters which are in the argument from
the receiver.

If you are sure that the string in rapidshare is well formed then you
could use something like

print rapidshare.gsub(%r{</?link>}, "")
or
rapidshare = rapidshare.gsub(%r{</?link>}, "")
print rapidshare

Putting it all together

require 'rubygems'
require 'scrubyt'

data = Scrubyt::Extractor.define do
fetch 'http://www.mywebsite.com/index.php?action=login'
fill_textfield 'user', 'myusername'
fill_textfield 'passwrd', 'mypassword'
submit
fetch 'http://mywebsite/index.php?topic=672'
link '//div/code'
end

rapidshare = data.to_xml
rapidshare.write($stdout, 1)
print rapidshare.gsub(%r{</?link>}, "")


--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
Ad

Advertisements

J

Juo Hundred

wow, thanks for such a comprehensive reply, and working code. You’re a
true gentleman :)
Thanks! :D
 

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

Similar Threads


Top