[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

String delete method help

Keith Johnston

10/1/2008 10:30:00 AM

Hi,

I have a string which hold a URL. I'm trying to use the string .delete
method to remove the domain from the url but it isn't working as I
thought it should from the docs.

For example:

string = "http://groups.google.com/group/ruby-talk-goo...

I want to remove the "http://groups.google... part of the string, so
I tried:

string.delete('http', 'com')

thinking this would delete everything between 'http' and 'com' but all
I get is the entire string without any changes and with no errors.

I'm probably misunderstanding how the delete function works - can
someone explain?

Thanks,

4 Answers

Stefano Crocco

10/1/2008 11:58:00 AM

0

Alle Wednesday 01 October 2008, Keith Johnston ha scritto:
> Hi,
>
> I have a string which hold a URL. I'm trying to use the string .delete
> method to remove the domain from the url but it isn't working as I
> thought it should from the docs.
>
> For example:
>
> string = "http://groups.google.com/group/ruby-talk-goo...
>
> I want to remove the "http://groups.google... part of the string, so
> I tried:
>
> string.delete('http', 'com')
>
> thinking this would delete everything between 'http' and 'com' but all
> I get is the entire string without any changes and with no errors.
>
> I'm probably misunderstanding how the delete function works - can
> someone explain?
>
> Thanks,

There are two reasons for which your code doesn't do what you want:
* String#delete is a non-destructive method, that is it doesn't change its
receiver but creates a new string which is a copy of the receiver, modifies it
and returns it:

str = "abc"
str1 = str.delete 'a'
puts str
=> "abc"
puts str1
=> "bc"

If you want to modify the string in place, use String#delete!, instead

* String#delete removes from the string the characters which are the
intersection between its arguments. This means that a) it works character-
wise, so calling str.delete("abc") will delete all 'a', all 'b' and all 'c',
not just all instances of the substring 'abc'; b) in your case, the two
strings you pass to delete have no characters in common, so none will be
removed.

One way to do what you want is to use sub!:

string = "http://groups.google.com/group/ruby-talk-goo...
string.sub!("http://groups.google..., '')

I hope this helps

Stefano

Lars Haugseth

10/1/2008 12:54:00 PM

0

* Keith Johnston <keith@kjohnston.co.uk> wrote:
>
> Hi,
>
> I have a string which hold a URL. I'm trying to use the string .delete
> method to remove the domain from the url but it isn't working as I
> thought it should from the docs.
>
> For example:
>
> string = "http://groups.google.com/group/ruby-talk-goo...
>
> I want to remove the "http://groups.google... part of the string, so
> I tried:
>
> string.delete('http', 'com')

No need to reinvent the wheel:

require 'uri'
uri = URI.parse('http://groups.google.com/group/ruby-talk-go...)
puts uri.path

http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classe...

--
Lars Haugseth

"If anyone disagrees with anything I say, I am quite prepared not only to
retract it, but also to deny under oath that I ever said it." -Tom Lehrer

Keith Johnston

10/1/2008 12:58:00 PM

0

Hi,

Yes, that helped. I was able to achieve what I thought the delete
function was doing by using String#sub! with a regular expression to
remove any charaters between "http" and "com".

string =3D "http://groups.google.com/group/ruby-talk-google/...!(/
http.*com/, "")


Thanks,

On Oct 1, 12:58=A0pm, Stefano Crocco <stefano.cro...@alice.it> wrote:
> Alle Wednesday 01 October 2008, Keith Johnston ha scritto:
>
>
>
> > Hi,
>
> > I have a string which hold a URL. I'm trying to use the string .delete
> > method to remove the domain from the url but it isn't working as I
> > thought it should from the docs.
>
> > For example:
>
> > string =3D "http://groups.google.com/group/ruby-talk-goo...
>
> > I want to remove the "http://groups.google... part of the string, so
> > I tried:
>
> > string.delete('http', 'com')
>
> > thinking this would delete everything between 'http' and 'com' but all
> > I get is the entire string without any changes and with no errors.
>
> > I'm probably misunderstanding how the delete function works - can
> > someone explain?
>
> > Thanks,
>
> There are two reasons for which your code doesn't do what you want:
> * String#delete is a non-destructive method, that is it doesn't change it=
s =A0
> receiver but creates a new string which is a copy of the receiver, modifi=
es it
> and returns it:
>
> str =3D "abc"
> str1 =3D str.delete 'a'
> puts str
> =3D> "abc"
> puts str1
> =3D> "bc"
>
> If you want to modify the string in place, use String#delete!, instead
>
> * String#delete removes from the string the characters which are the
> intersection between its arguments. This means that a) it works character=
-
> wise, so calling str.delete("abc") will delete all 'a', all 'b' and all '=
c',
> not just all instances of the substring 'abc'; b) in your case, the two
> strings you pass to delete have no characters in common, so none will be
> removed.
>
> One way to do what you want is to use sub!:
>
> string =3D "http://groups.google.com/group/ruby-talk-goo...
> string.sub!("http://groups.google..., '')
>
> I hope this helps
>
> Stefano

David A. Black

10/1/2008 1:41:00 PM

0

Hi --

On Wed, 1 Oct 2008, Keith Johnston wrote:

> Hi,
>
> Yes, that helped. I was able to achieve what I thought the delete
> function was doing by using String#sub! with a regular expression to
> remove any charaters between "http" and "com".
>
> string = "http://groups.google.com/group/ruby-talk-google/...!(/
> http.*com/, "")

It's a bit dangerous to chain sub! (and, indeed, the ! means that this
is the dangerous version of sub, so it's always a good idea to check
the docs, since "danger" can mean many things).

str = "hello".sub!(/x/, 'y')
=> nil

That won't happen with your string but I'd still tend to steer clear
of assigning directly from a sub! call.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.r... for details and updates!