A method to modify link url in ruby -
i want make method named url_change. returns url after (#) removed. example:
url_change('www.localhost.com#about') # returns 'www.localhost.com'
previously, did this,but not working well.
def url_change(url) if url.include? '#' url.slice!(/#./) return url else return url end end
and also,
def url_change(url) url.split("#") end
what's problem here?? please show me way?
you should use pre-existing tools:
require 'uri' def url_change(url) uri = uri.parse(url) uri.fragment = nil uri.to_s end url_change('http://www.example.com#1') # => "http://www.example.com"
it works whether or not url contains scheme:
url_change('www.example.com#1') # => "www.example.com"
uri part of ruby's standard library, , tested.
Comments
Post a Comment