Project

General

Profile

Actions

Feature #13665

closed

String#delete_suffix

Added by sonots (Naotoshi Seo) almost 7 years ago. Updated almost 7 years ago.

Status:
Closed
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?

Updated by nobu (Nobuyoshi Nakada) almost 7 years 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.

Updated by shevegen (Robert A. Heiler) almost 7 years ago

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

Updated by sonots (Naotoshi Seo) almost 7 years 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.

Updated by matz (Yukihiro Matsumoto) almost 7 years ago

Looks good to me.

Matz.

Actions #5

Updated by sonots (Naotoshi Seo) almost 7 years 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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0