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

Also available in: Atom PDF

Like0
Like0