删除 Ruby 中 URI 的最后一段 [英] Remove last segment of an URI in Ruby

查看:38
本文介绍了删除 Ruby 中 URI 的最后一段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Ruby 删除给定 URI 的最后一段,

I'm trying to remove the last segment of a given URI using Ruby,

像这样:

http://example.com/foo/bar/baz/?lala=foo

我怎样才能得到这个:

"http://example.com/foo/bar/baz/"

我已经搜索过了,我得到的只是获取 URI 的最后一个段或主机部分.

I've searched and all that I got is to get the last segment or the host part of the URI.

推荐答案

根据您对 @JorgWMittag 的评论,您真正需要的是这个

Based on your comment to @JorgWMittag all your really need is this

s = 'http://example.com/foo/bar/baz/?lala=foo#quux'
s[/.*\//]
#=> "http://example.com/foo/bar/baz/"
s = 'http://example.com/foo/bar.html'
s[/.*\//]
#=> "http://example.com/foo/"

基本上它只是说如果你有像

Basically it just says grab the substring up to the last slash obviously if you have things like

s = "http://example.com/foo/bar/?baz=/blah/blah"

那么这行不通,但目前您的问题和规格似乎很松散.

Then this will not work but your question and specifications seem very loose at the moment.

这篇关于删除 Ruby 中 URI 的最后一段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆