From: "wardrop (Tom Wardrop)" Date: 2012-08-08T10:52:07+09:00 Subject: [ruby-core:47063] [ruby-trunk - Feature #6842] Add Optional Arguments to String#strip Issue #6842 has been updated by wardrop (Tom Wardrop). =begin In the first example, any of the characters in the string can match. So "<", "/", "b", and ">". It stripped the "b" from the start of the string, and the "" from the end. This is how I remember trim() from php behaved, which I found quite succinct, but maybe that's because defining arrays in PHP is fairly verbose. Ruby has the shorthand array literal syntax (({%w{< / b >}})), so I don't mind if #strip was made to treat a single argument and an array of arguments the same, rather than treating a single string and an array of characters. Actually I think I'd prefer that API - certainly less confusing. So in that case, the out of the original example would become: "bold text".strip("") #=> "bold text" And you'd do the following to achieve the same result as the original example: "bold text".strip(%w{< / b >}) #=> "old text" As for differentiating left and right, isn't that what #lstrip and #rstrip are for? You could easily chain them to get the desired result. E.g. "bold text".lstrip("").rstrip("") #=> "old text" =end ---------------------------------------- Feature #6842: Add Optional Arguments to String#strip https://bugs.ruby-lang.org/issues/6842#change-28717 Author: wardrop (Tom Wardrop) Status: Open Priority: Normal Assignee: Category: core Target version: 2.0.0 =begin One of the very few things I miss from the ol' php days many years ago, was the ability to easily strip arbitrary characters from the ends of string using trim(). Typically, this is whitespace characters, and #strip currently fulfils that use case, but there are also instances where it'd be nice to be able to strip any range of characters from the ends of a string. It goes well with Array#join as often when joining strings with a delimiter, you want to make sure those strings don't already begin or end with that character. For a full-featured #strip, I'd like to see it have the option of accepting both an Array or String. If a string is provided, each character in that string will be stripped. If an array of strings is given, each element of the array is stripped from the ends of the string - this allows for multi-character delimiters for example. Of course you could go really nuts and supports regex as well (or instead of arrays). To demonstrate the difference... "bold text".strip("") #=> "old text" "bold text".strip(["", ""]) #=> "bold text" "bold text".strip(["", "", "", ""]) #=> "bold text" "bold text".strip(/<\/?.+?>/) #=> "bold text" A simple real-world example; this is actually what I was wanting to do right before I came here to raise this feature request, but there's been all kinds of other use cases I've hit in the past: ['some', '/chunked', 'path/'].map{ |v| v.strip('/') }.join('/') #=> "some/chunked/path" File#join does something similar, but when you need control over the joining character, this is the way you'd do it. I've lost count of how many times I've wanted this in Ruby, and there's really no nice workaround. Here's an example on StackOverflow of someone asking how to achieve this stripping behaviour in ruby: http://stackoverflow.com/questions/3453262/how-to-strip-leading-and-trailing-quote-from-string-in-ruby Obviously, you'd do the same for #lstrip and #rstrip, and all the mutable variants (#strip!, #lstrip!, #rstrip!). Looking forward to others thoughts on this one. =end -- http://bugs.ruby-lang.org/