I'm trying to remove the last segment of a given URI using Ruby,
like this:
http://example.com/foo/bar/baz/?lala=foo
How can I get this :
"http://example.com/foo/bar/baz/"
I've searched and all that I got is to get the last segment or the host part of the URI.
Based on your comment to @JorgWMittag all your really need is this
Basically it just says grab the substring up to the last slash obviously if you have things like
Then this will not work but your question and specifications seem very loose at the moment.
Did you tried something like this, possibly the first thing when you want to split a string
The most important thing to remember is: Ruby is an object-oriented language. It's not an array-oriented language, it's not a hash-oriented language, it's not a string-oriented language.
When you want to do something, you construct an object which represents your concept and manipulate that object. In this case, you want to manipulate a URI, so you need to construct an object which represents a URI.
Thankfully, the Ruby standard library already contains a ready-made class for such objects:
As you can see, once you create a proper object representing your URI, manipulating it becomes trivial.