Project

General

Profile

Feature #13665

String#delete_suffix

Added by sonots (Naotoshi Seo) 5 months ago. Updated 4 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:81714]

Description

In https://bugs.ruby-lang.org/issues/12694, we decided to have String#delete_prefix.
It would be nice to have String#delete_suffix method to delete suffix from a string for symmetry although it can be achieved with String#chomp as:

class String
  def delete_suffix(substr)
    chomp(substr)
  end
  def delete_suffix!(substr)
    chomp!(substr)
  end
end

Any opinions?

Associated revisions

Revision 59377
Added by sonots (Naotoshi Seo) 4 months ago

string.c: add String#delete_suffix and String#delete_suffix!
to remove trailing suffix [Feature #13665] [Fix GH-1661]

  • string.c (rb_str_delete_suffix_bang): add a new method
    to remove suffix destuctively.

  • string.c (rb_str_delete_suffix): add a new method
    to remove suffix non-destuctively.

  • test/ruby/test_string.rb: add tests.

History

#1 [ruby-core:81716] Updated by nobu (Nobuyoshi Nakada) 5 months ago

  • Description updated (diff)

At yesterday meeting, a name lchomp was proposed but rejected because of the special deal of CR LF.
If delete_suffix will be introduced, it should keep CR before LF when the argument is LF only, as well as delete_prefix.

#2 [ruby-core:81735] Updated by shevegen (Robert A. Heiler) 5 months ago

I think the method's functionality is good to have.

#3 [ruby-core:81772] Updated by sonots (Naotoshi Seo) 5 months ago

I've sent a PR https://github.com/ruby/ruby/pull/1661.
I will merge if I get agreement at a ruby developer's meeting.

#4 [ruby-core:82045] Updated by matz (Yukihiro Matsumoto) 4 months ago

Looks good to me.

Matz.

#5 Updated by sonots (Naotoshi Seo) 4 months ago

  • Status changed from Open to Closed

Applied in changeset trunk|r59377.


string.c: add String#delete_suffix and String#delete_suffix!
to remove trailing suffix [Feature #13665] [Fix GH-1661]

  • string.c (rb_str_delete_suffix_bang): add a new method
    to remove suffix destuctively.

  • string.c (rb_str_delete_suffix): add a new method
    to remove suffix non-destuctively.

  • test/ruby/test_string.rb: add tests.

Also available in: Atom PDF