# Deleted unwanted characters from a string

**URL:** <https://www.ruby-forum.com/t/deleted-unwanted-characters-from-a-string/170483>\
**Category:** Ruby\
**Created:** [June 18, 2009, 1:50am UTC](https://www.ruby-forum.com/t/deleted-unwanted-characters-from-a-string/170483 "2009-06-18T01:50:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![juo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@juo](https://www.ruby-forum.com/u/juo)\
**Post date:** [June 18, 2009, 1:50am UTC](https://www.ruby-forum.com/t/deleted-unwanted-characters-from-a-string/170483/1 "2009-06-18T01:50:23Z")

</div>

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

`<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](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 ‘[mywebsite.com is available for purchase - Sedo.com](http://www.mywebsite.com/index.php?action=login)’  
fill\_textfield ‘user’, ‘myusername’  
fill\_textfield ‘passwrd’, ‘mypassword’  
submit  
fetch ‘[http://mywebsite/index.php?topic=672](http://mywebsite/index.php?topic=672)’  
link ‘//div/code’  
end

rapidshare = data.to\_xml.write($stdout, 1)  
rapidshare.to\_s  
rapidshare.delete “”  
print rapidshare  
[/code]

Any help would be great appreciated 🙂

is my full code

---

<div class="post-metadata">

**Author:** ![juo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@juo](https://www.ruby-forum.com/u/juo)\
**Post date:** [June 18, 2009, 4:20am UTC](https://www.ruby-forum.com/t/deleted-unwanted-characters-from-a-string/170483/2 "2009-06-18T04:20:45Z")

</div>

Juo H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> rapidshare.delete “”

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

rapidshare = rapidshare.delete(whatever)

---

<div class="post-metadata">

**Author:** ![juo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@juo](https://www.ruby-forum.com/u/juo)\
**Post date:** [June 18, 2009, 12:58pm UTC](https://www.ruby-forum.com/t/deleted-unwanted-characters-from-a-string/170483/3 "2009-06-18T12:58:18Z")

</div>

Hey, I tried that

```auto
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”

---

<div class="post-metadata">

**Author:** ![juo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@juo](https://www.ruby-forum.com/u/juo)\
**Post date:** [June 18, 2009, 4:30pm UTC](https://www.ruby-forum.com/t/deleted-unwanted-characters-from-a-string/170483/4 "2009-06-18T16:30:53Z")

</div>

wow, thanks for such a comprehensive reply, and working code. Youâ€™re a  
true gentleman 🙂  
Thanks! 😃

---

<div class="post-metadata">

**Author:** ![juo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@juo](https://www.ruby-forum.com/u/juo)\
**Post date:** [June 18, 2009, 4:20pm UTC](https://www.ruby-forum.com/t/deleted-unwanted-characters-from-a-string/170483/5 "2009-06-18T16:20:34Z")

</div>

On Thu, Jun 18, 2009 at 6:58 AM, Juo H.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey, I tried that
> 
> [code]  
> rapidshare = data.to\_xml.write($stdout, 1)

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:

> rapidshare.to\_s  
> 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,

> rapidshare = rapidshare.delete(\<“link”\>)

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(“”)  
=\> "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 ‘[mywebsite.com is available for purchase - Sedo.com](http://www.mywebsite.com/index.php?action=login)’  
fill\_textfield ‘user’, ‘myusername’  
fill\_textfield ‘passwrd’, ‘mypassword’  
submit  
fetch ‘[http://mywebsite/index.php?topic=672](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/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
