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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -