# Urls from gsub not correct

**URL:** <https://www.ruby-forum.com/t/urls-from-gsub-not-correct/182161>\
**Category:** Ruby\
**Created:** [January 23, 2010, 5:30pm UTC](https://www.ruby-forum.com/t/urls-from-gsub-not-correct/182161 "2010-01-23T17:30:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [January 23, 2010, 5:30pm UTC](https://www.ruby-forum.com/t/urls-from-gsub-not-correct/182161/1 "2010-01-23T17:30:57Z")

</div>

i have some problems when i try to check urls after get them from gsub  
method. ex

from the console:

(‘[http://ale.it](http://ale.it)’ =~ URI::regexp).nil?.to\_s  
=\> “false”

and it works fine but if i launch this:

“[http://ale.it](http://ale.it)”.gsub(/http[s]?://[^\s]+/, (‘\0’ =~  
URI::regexp).nil?.to\_s)  
=\> “true”

it doesn’t work.

How can i do, for get correct urls?

thanks

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [January 23, 2010, 5:45pm UTC](https://www.ruby-forum.com/t/urls-from-gsub-not-correct/182161/2 "2010-01-23T17:45:50Z")

</div>

On 01/23/2010 05:32 PM, Luca R. wrote:

> “[http://ale.it](http://ale.it)”.gsub(/http[s]?://[^\s]+/, (‘\0’ =~  
> URI::regexp).nil?.to\_s)  
> =\> “true”
> 
> it doesn’t work.

It cannot because your second match is evaluated _before_ gsub starts  
its work. So the value passed as second argument to gsub can never  
change.

> How can i do, for get correct urls?

The question is: what do you want to achieve with the code? It looks  
like you want to replace valid URL’s with “false” and invalid URL’s with  
“true”. That does not seem reasonable to me but if you want to do that  
you can use the block form of gsub like this:

“[http://ale.it](http://ale.it)”.gsub(/http[s]?://[^\s]+/) do |m|  
(m =~ URI::regexp).nil?.to\_s  
end

Kind regards

robert
