urls from gsub not correct

L

Luca Roma

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

from the console:

('http://ale.it' =~ URI::regexp).nil?.to_s
=> "false"

and it works fine but if i launch this:

"http://ale.it".gsub(/http?:\/\/[^\s]+/, ('\0' =~
URI::regexp).nil?.to_s)
=> "true"

it doesn't work.

How can i do, for get correct urls?

thanks
 
R

Robert Klemme

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

from the console:

('http://ale.it' =~ URI::regexp).nil?.to_s
=> "false"

and it works fine but if i launch this:

"http://ale.it".gsub(/http?:\/\/[^\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".gsub(/http?:\/\/[^\s]+/) do |m|
(m =~ URI::regexp).nil?.to_s
end

Kind regards

robert
 

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

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top