Ruby comes with a nice class for this called URI, so take advantage of it:
require 'uri'
uri = URI.parse('//www.example.com') # => #<URI::Generic:0x007ff0098581e8 URL://www.example.com>
uri.scheme = 'https' # => "https"
uri.to_s # => "https://www.example.com"
If you want to process a list of URLs:
%w[
//www.example.com
].map{ |url| # => ["//www.example.com"]
uri = URI.parse(url) # => #<URI::Generic:0x007ff009853350 URL://www.example.com>
uri.scheme = 'https' # => "https"
uri.to_s # => "https://www.example.com"
} # => ["https://www.example.com"]
The advantage of URI is it's smart enough to do the right thing if the URL already has a scheme or is missing it entirely:
require 'uri'
%w[
http://foo.com
https://foo.com
//foo.com
].map { |url|
uri = URI.parse(url)
uri.scheme = 'https'
uri.to_s
} # => ["https://foo.com", "https://foo.com", "https://foo.com"]
If you insist on using a regex, then simplify it:
url = '//www.example.com'
url[/^/] = 'https:'
url # => "https://www.example.com"
And:
%w[
//www.example.com
].map{ |url| # => ["//www.example.com"]
url[/^/] = 'https:' # => "https:"
url # => "https://www.example.com"
} # => ["https://www.example.com"]
Using a regular expression isn't smart enough to sense whether the scheme already exists, so more code has to be written to handle that situation.
scheme. See the example.