Muhammad Faisal Iqbal Muhammad Faisal Iqbal - 6 months ago 10
Ruby Question

String comparison like "abcDEF" == "*DEF"

I am working on an rails app in which I need a comparison like:

url == "http://localhost:3000/admin/admin_login"


I am wondering that is there a way to compare like:

url == "*/admin/admin_login"

nwk nwk
Answer

You can use a regexp.

irb(main):001:0> url = "http://localhost:3000/admin/admin_login"
=> "http://localhost:3000/admin/admin_login"
irb(main):002:0> url =~ /admin\/admin_login$/
=> 22
irb(main):003:0> url =~ /foo\/something_else$/
=> nil
irb(main):004:0> "http://localhost:3000/admin/admin_login/bar" =~ /admin\/admin_login$/
=> nil
Comments