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-google/”
I want to remove the “http://groups.google.com” 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,
Alle Wednesday 01 October 2008, Keith J. ha scritto:
I want to remove the “http://groups.google.com” part of the string, so
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-google/”
string.sub!(“http://groups.google.com”, ‘’)
I hope this helps
Stefano
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/”.sub!(/
http.*com/, “”)
Thanks,
I want to remove the “http://groups.google.com” 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-google/’)
puts uri.path
http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI.html
–
Lars H.
“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
Hi –
On Wed, 1 Oct 2008, Keith J. 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/”.sub!(/
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