views:

54

answers:

2

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]?:\/\/[^\s]+/, ('\0' =~ URI::regexp).nil?.to_s)   
=> "true"

it doesn't work.

How can i do, for get correct urls?

thanks

+3  A: 

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.

mikej
A: 

i solved with:

"http://ale.it".gsub(/http[s]?:\/\/[^\s]+/) do |m|
 (m =~ URI::regexp).nil?.to_s) 
end
Luca Romagnoli
Am glad you solved it but am still not totally clear what you were trying to do :)
mikej