Project

General

Profile

Actions

Feature #18640

open

default empty string argument for `String#sub` and `String#sub!`, e.g. `"hello".sub("l")`

Added by dorianmariefr (Dorian MariƩ) over 2 years ago. Updated over 2 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:107955]

Description

Most of the time I replace a string with an empty string. And I think that's the main use case.

Also, String#delete deletes every character provided when passed a string.

class String
  alias original_sub sub
  alias original_sub! sub!

  def sub(pattern, replacement = "", &block)
    original_sub(pattern, replacement, &block)
  end

  def sub!(pattern, replacement = "", &block)
    original_sub!(pattern, replacement, &block)
  end
end

puts "hello".sub("l")
puts "hello".sub!("l")

What do you think?

Actions #1

Updated by nobu (Nobuyoshi Nakada) over 2 years ago

  • Tracker changed from Bug to Feature
  • ruby -v deleted (ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x86_64-darwin21])
  • Backport deleted (2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN)
Actions

Also available in: Atom PDF

Like0
Like0