Answer the question
In order to leave comments, you need to log in
How to get part of a string in ruby?
There is a line
string = 'https://qna.habr.com/question/new'
You need to get everything between the slashes with the help of ruby, in this case it should work qna.habr.com
if the site came in this form qna.habr.com/question/new
anyway, get it anyway , qna.habr.com
or if qna.habr.com
it doesn’t matter. qna.habr.com
Advise how this can be done?
Answer the question
In order to leave comments, you need to log in
Certainly
require 'uri'
def extract_domain(url)
url = "http://#{url}" if URI.parse(url).scheme.nil?
URI.parse(url).host.downcase
end
extract_domain('https://qna.habr.com/question/new') #=> qna.habr.com
extract_domain('qna.habr.com/question/new') #=> qna.habr.com
extract_domain('qna.habr.com') #=> qna.habr.com
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question