Forum: Ruby-core [ruby-trunk - Feature #6842][Open] Add Optional Arguments to String#strip

75ffb1ee2ccd32713b37a401973162dd?d=identicon&s=25 Tom Wardrop (wardrop)
on 2012-08-07 13:27
(Received via mailing list)
Issue #6842 has been reported by wardrop (Tom Wardrop).

----------------------------------------
Feature #6842: Add Optional Arguments to String#strip
https://bugs.ruby-lang.org/issues/6842

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...

    "<b>bold text</b>".strip("</b>") #=> "old text"
    "<b>bold text</b>".strip(["<b>", "</b>"]) #=> "bold text"
    "<em><b>bold text</b></em>".strip(["<b>", "</b>", "<em>", "</em>"])
#=> "bold text"
    "<em><b>bold text</b></em>".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-...

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
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Thomas Sawyer (7rans)
on 2012-08-07 16:10
(Received via mailing list)
Issue #6842 has been updated by trans (Thomas Sawyer).


The first example doesn't seem to make sense, e.g.

  "<b>bold text</b>".strip("</b>") #=> "old text"

I also think the interface needs to be a bit more specific about right
and left stripping. Maybe use options:

  strip([String,Array])  # both left and right
  strip(:left=>[String,Array], :right=>[String,Array])  # selective
right vs left



----------------------------------------
Feature #6842: Add Optional Arguments to String#strip
https://bugs.ruby-lang.org/issues/6842#change-28711

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...

    "<b>bold text</b>".strip("</b>") #=> "old text"
    "<b>bold text</b>".strip(["<b>", "</b>"]) #=> "bold text"
    "<em><b>bold text</b></em>".strip(["<b>", "</b>", "<em>", "</em>"])
#=> "bold text"
    "<em><b>bold text</b></em>".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-...

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
75ffb1ee2ccd32713b37a401973162dd?d=identicon&s=25 Tom Wardrop (wardrop)
on 2012-08-08 03:53
(Received via mailing list)
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>b" from the start of the
string, and the "</b>" 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:

    "<b>bold text</b>".strip("</b>") #=> "<b>bold text"

And you'd do the following to achieve the same result as the original
example:

    "<b>bold text</b>".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.

    "<b>bold text</b>".lstrip("<b>").rstrip("</b>") #=> "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...

    "<b>bold text</b>".strip("</b>") #=> "old text"
    "<b>bold text</b>".strip(["<b>", "</b>"]) #=> "bold text"
    "<em><b>bold text</b></em>".strip(["<b>", "</b>", "<em>", "</em>"])
#=> "bold text"
    "<em><b>bold text</b></em>".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-...

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
F24ff61beb80aa5f13371aa22a35619c?d=identicon&s=25 mame (Yusuke Endoh) (Guest)
on 2012-10-12 14:19
(Received via mailing list)
Issue #6842 has been updated by mame (Yusuke Endoh).

Status changed from Open to Assigned
Assignee set to matz (Yukihiro Matsumoto)


----------------------------------------
Feature #6842: Add Optional Arguments to String#strip
https://bugs.ruby-lang.org/issues/6842#change-30432

Author: wardrop (Tom Wardrop)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
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...

    "<b>bold text</b>".strip("</b>") #=> "old text"
    "<b>bold text</b>".strip(["<b>", "</b>"]) #=> "bold text"
    "<em><b>bold text</b></em>".strip(["<b>", "</b>", "<em>", "</em>"])
#=> "bold text"
    "<em><b>bold text</b></em>".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-...

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
F24ff61beb80aa5f13371aa22a35619c?d=identicon&s=25 mame (Yusuke Endoh) (Guest)
on 2012-11-24 00:47
(Received via mailing list)
Issue #6842 has been updated by mame (Yusuke Endoh).

Target version changed from 2.0.0 to next minor


----------------------------------------
Feature #6842: Add Optional Arguments to String#strip
https://bugs.ruby-lang.org/issues/6842#change-33674

Author: wardrop (Tom Wardrop)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


=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...

    "<b>bold text</b>".strip("</b>") #=> "old text"
    "<b>bold text</b>".strip(["<b>", "</b>"]) #=> "bold text"
    "<em><b>bold text</b></em>".strip(["<b>", "</b>", "<em>", "</em>"])
#=> "bold text"
    "<em><b>bold text</b></em>".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-...

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
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Thomas Sawyer (7rans)
on 2012-11-24 04:31
(Received via mailing list)
Issue #6842 has been updated by trans (Thomas Sawyer).


Might support regexp instead of arrays,

  "<b>bold text</b>".strip(/[\/<b>]/) #=> "old text"

Also note, we partially have this for #rstrip already in the form of
#chomp.
----------------------------------------
Feature #6842: Add Optional Arguments to String#strip
https://bugs.ruby-lang.org/issues/6842#change-33762

Author: wardrop (Tom Wardrop)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


=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...

    "<b>bold text</b>".strip("</b>") #=> "old text"
    "<b>bold text</b>".strip(["<b>", "</b>"]) #=> "bold text"
    "<em><b>bold text</b></em>".strip(["<b>", "</b>", "<em>", "</em>"])
#=> "bold text"
    "<em><b>bold text</b></em>".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-...

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
Bcb6acc9d0d9bef99e033b36c3d32ca9?d=identicon&s=25 charliesome (Charlie Somerville) (Guest)
on 2012-11-24 05:08
(Received via mailing list)
Issue #6842 has been updated by charliesome (Charlie Somerville).


I don't agree with the options hash that was proposed for allowing
separate strings to be stripped from the left and the right.

This:

  str.strip(:left => "foo", :right => "bar")

is not as clean as something like this IMO:

  str.lstrip("foo").rstrip("bar")
----------------------------------------
Feature #6842: Add Optional Arguments to String#strip
https://bugs.ruby-lang.org/issues/6842#change-33775

Author: wardrop (Tom Wardrop)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


=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...

    "<b>bold text</b>".strip("</b>") #=> "old text"
    "<b>bold text</b>".strip(["<b>", "</b>"]) #=> "bold text"
    "<em><b>bold text</b></em>".strip(["<b>", "</b>", "<em>", "</em>"])
#=> "bold text"
    "<em><b>bold text</b></em>".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-...

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
75ffb1ee2ccd32713b37a401973162dd?d=identicon&s=25 Tom Wardrop (wardrop)
on 2013-11-17 05:49
(Received via mailing list)
Issue #6842 has been updated by wardrop (Tom Wardrop).


I still look forward to this feature. So much more convenient and
readable than the current work-around of using regex and sub/gsub. Is
there anything more that needs to be done to make this happened for Ruby
2.1 or 2.2?
----------------------------------------
Feature #6842: Add Optional Arguments to String#strip
https://bugs.ruby-lang.org/issues/6842#change-42982

Author: wardrop (Tom Wardrop)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


=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...

    "<b>bold text</b>".strip("</b>") #=> "old text"
    "<b>bold text</b>".strip(["<b>", "</b>"]) #=> "bold text"
    "<em><b>bold text</b></em>".strip(["<b>", "</b>", "<em>", "</em>"])
#=> "bold text"
    "<em><b>bold text</b></em>".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-...

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
22e767367fe9c51fc5d22af7a631c424?d=identicon&s=25 Fuad Saud (Guest)
on 2013-11-17 05:58
(Received via mailing list)
I need this a couple of weeks ago. Looks like a preety commo use case.
This topic is locked and can not be replied to.