获取url的尾部斜杠后面的值

时间:2014-09-30 13:20:06

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

我有一个像这样的简单网址

  

http://siteurl.com/2014/09/net-special-report-9ice-father-of-3-successful-entertainer-with-eyes-on-house-of-reps/

如何在尾部斜杠之后得到最后一个值

  

净专题报导-9ice - 父亲的-3成功的,艺人与 - 眼睛上,房子的,代表

任何想法。感谢

1 个答案:

答案 0 :(得分:1)

使用拆分执行此操作。如下所示:

str = "http://siteurl.com/2014/09/net-special-report-9ice-father-of-3-successful-entertainer-with-eyes-on-house-of-reps/"
#=> "http://siteurl.com/2014/09/net-special-report-9ice-father-of-3-successful-entertainer-with-eyes-on-house-of-reps/" 
a = str.split('/')
#=> ["http:", "", "siteurl.com", "2014", "09", "net-special-report-9ice-father-of-3-successful-entertainer-with-eyes-on-house-of-reps"] 
a.last
#=> "net-special-report-9ice-father-of-3-successful-entertainer-with-eyes-on-house-of-reps"