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Ʃ) 3 months ago. Updated 3 months ago.

Status:
Open
Priority:
Normal
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) 3 months ago

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

Also available in: Atom PDF