Reputation: 12475
I have some problems when I try to check urls after I get them using the gsub method.
From the console it works fine:
('http://ale.it' =~ URI::regexp).nil?.to_s
=> "false"
but if i launch this it doesn't work:
"http://ale.it".gsub(/http[s]?:\/\/[^\s]+/, ('\0' =~ URI::regexp).nil?.to_s)
=> "true"
How can I get correct urls?
Upvotes: 1
Views: 576
Reputation: 12475
i solved with:
"http://ale.it".gsub(/http[s]?:\/\/[^\s]+/) do |m|
(m =~ URI::regexp).nil?.to_s)
end
Upvotes: 0
Reputation: 66293
This is an explanation of what your 2 examples do. Whilst it isn't really an answer it's a bit long to fit in a comment.
=~
returns the position where a match occurs or nil
if no match is found.
In your first example 'http://ale.it' matches URI::regexp
starting at position 0 so you get 0.nil?
which is false, converted to a string "false"
gsub
in your second example takes 2 parameters, a pattern and a replacement string and replaces all matches of the pattern with the replacement.
'\0' doesn't match URI::regexp
so ('\0' =~ URI::regexp).nil?
is true and with to_s
applied is the string "true".
"http://ale.it" matches /http[s]?:\/\/[^\s]+/
so gets replaced with "true".
You will have to expand your question to explain what you're trying to achieve.
Upvotes: 3