简体   繁体   中英

Compare two different string with the same part in Ruby on Rails

I need to accept URL and compare it with another. Both URL's have the same start but different end which dynamically changes. I accept something like this https://url.com/something_else and need to compare with https://url.com/ check if this part is in received URL without something_else . How can I do this in Rails?

You can use URI module https://docs.ruby-lang.org/en/2.1.0/URI.html

uri = URI("http://foo.com/posts?id=30&limit=5#time=1305298413")
uri.host
#=> "foo.com"

Please try the URI Module as given below:-

require 'uri'

uri = URI("http://foo.com/posts?id=30&limit=5#time=1305298413")
#=> #<URI::HTTP:0x00000000b14880
      URL:http://foo.com/posts?id=30&limit=5#time=1305298413>
uri.scheme
#=> "http"
uri.host
#=> "foo.com"
uri.path
#=> "/posts"
uri.query
#=> "id=30&limit=5"
uri.fragment
#=> "time=1305298413"

uri.to_s
#=> "http://foo.com/posts?id=30&limit=5#time=1305298413"

Thanks :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM