ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:119883] [Ruby master Feature#20885] String#gsub?
@ 2024-11-12  4:11 Dan0042 (Daniel DeLorme) via ruby-core
  2024-12-18  6:11 ` [ruby-core:120289] " matz (Yukihiro Matsumoto) via ruby-core
  2024-12-18 17:28 ` [ruby-core:120301] " Dan0042 (Daniel DeLorme) via ruby-core
  0 siblings, 2 replies; 3+ messages in thread
From: Dan0042 (Daniel DeLorme) via ruby-core @ 2024-11-12  4:11 UTC (permalink / raw)
  To: ruby-core; +Cc: Dan0042 (Daniel DeLorme)

Issue #20885 has been reported by Dan0042 (Daniel DeLorme).

----------------------------------------
Feature #20885: String#gsub?
https://bugs.ruby-lang.org/issues/20885

* Author: Dan0042 (Daniel DeLorme)
* Status: Open
----------------------------------------
I would like a variation of sub/gsub that returns a new string if there is a match, or nil otherwise.

This can be currently done with `str.dup.gsub!(...)` at the mere cost of 2 unnecessary extra allocations due to String#dup

TBH I'm not sure we need yet another gsub method, but what gets me here is that `gsub?` is the primitive operation from which `gsub` and `gsub!` are made. Internally, these two methods work like this:

```ruby
def gsub!(...)
  modified = gsub?(...) and replace(modified)
end

def gsub(...)
  gsub?(...) or dup
end
```

We can efficiently derive these two methods from `gsub?`, but we cannot efficiently implement `gsub?` in ruby, so it feels regrettable to me that `gsub?` is not available as a built-in.

Use cases include:

```ruby
#ensure gsub has modified the string
newstr = str.gsub?(rx,repl) or raise "str did not contain expected value"

#take an action if the string is modified
if str = obj.title.gsub?(rx,repl)
  obj.title = str
  obj.save
end
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:120289] [Ruby master Feature#20885] String#gsub?
  2024-11-12  4:11 [ruby-core:119883] [Ruby master Feature#20885] String#gsub? Dan0042 (Daniel DeLorme) via ruby-core
@ 2024-12-18  6:11 ` matz (Yukihiro Matsumoto) via ruby-core
  2024-12-18 17:28 ` [ruby-core:120301] " Dan0042 (Daniel DeLorme) via ruby-core
  1 sibling, 0 replies; 3+ messages in thread
From: matz (Yukihiro Matsumoto) via ruby-core @ 2024-12-18  6:11 UTC (permalink / raw)
  To: ruby-core; +Cc: matz (Yukihiro Matsumoto)

Issue #20885 has been updated by matz (Yukihiro Matsumoto).


I am not positive for adding `gsub?`. It's not a predicate, after all.

Matz.


----------------------------------------
Feature #20885: String#gsub?
https://bugs.ruby-lang.org/issues/20885#change-111054

* Author: Dan0042 (Daniel DeLorme)
* Status: Open
----------------------------------------
I would like a variation of sub/gsub that returns a new string if there is a match, or nil otherwise.

This can be currently done with `str.dup.gsub!(...)` at the mere cost of 2 unnecessary extra allocations due to String#dup

TBH I'm not sure we need yet another gsub method, but what gets me here is that `gsub?` is the primitive operation from which `gsub` and `gsub!` are made. Internally, these two methods work like this:

```ruby
def gsub!(...)
  modified = gsub?(...) and replace(modified)
end

def gsub(...)
  gsub?(...) or dup
end
```

We can efficiently derive these two methods from `gsub?`, but we cannot efficiently implement `gsub?` in ruby, so it feels regrettable to me that `gsub?` is not available as a built-in.

Use cases include:

```ruby
# ensure gsub has modified the string
newstr = str.gsub?(rx,repl) or raise "str did not contain expected value"

# take an action if the string is modified
if str = obj.title.gsub?(rx,repl)
  obj.title = str
  obj.save
end

# avoid allocating a new string if there is no change
str = str.gsub?(rx,repl) || str
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:120301] [Ruby master Feature#20885] String#gsub?
  2024-11-12  4:11 [ruby-core:119883] [Ruby master Feature#20885] String#gsub? Dan0042 (Daniel DeLorme) via ruby-core
  2024-12-18  6:11 ` [ruby-core:120289] " matz (Yukihiro Matsumoto) via ruby-core
@ 2024-12-18 17:28 ` Dan0042 (Daniel DeLorme) via ruby-core
  1 sibling, 0 replies; 3+ messages in thread
From: Dan0042 (Daniel DeLorme) via ruby-core @ 2024-12-18 17:28 UTC (permalink / raw)
  To: ruby-core; +Cc: Dan0042 (Daniel DeLorme)

Issue #20885 has been updated by Dan0042 (Daniel DeLorme).


Ah, you mean it doesn't return strictly true/false? That's a good point, although there are precedents like `Numeric#nonzero?`

But I'm not so attached to the name "gsub?" itself, it just happened to be the one that made the most sense to me. We could go with anything really. But I'm interested in discussing if this method, under another name, has a place in Ruby core.

----------------------------------------
Feature #20885: String#gsub?
https://bugs.ruby-lang.org/issues/20885#change-111067

* Author: Dan0042 (Daniel DeLorme)
* Status: Open
----------------------------------------
I would like a variation of sub/gsub that returns a new string if there is a match, or nil otherwise.

This can be currently done with `str.dup.gsub!(...)` at the mere cost of 2 unnecessary extra allocations due to String#dup

TBH I'm not sure we need yet another gsub method, but what gets me here is that `gsub?` is the primitive operation from which `gsub` and `gsub!` are made. Internally, these two methods work like this:

```ruby
def gsub!(...)
  modified = gsub?(...) and replace(modified)
end

def gsub(...)
  gsub?(...) or dup
end
```

We can efficiently derive these two methods from `gsub?`, but we cannot efficiently implement `gsub?` in ruby, so it feels regrettable to me that `gsub?` is not available as a built-in.

Use cases include:

```ruby
# ensure gsub has modified the string
newstr = str.gsub?(rx,repl) or raise "str did not contain expected value"

# take an action if the string is modified
if str = obj.title.gsub?(rx,repl)
  obj.title = str
  obj.save
end

# avoid allocating a new string if there is no change
str = str.gsub?(rx,repl) || str
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-12-18 17:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-12  4:11 [ruby-core:119883] [Ruby master Feature#20885] String#gsub? Dan0042 (Daniel DeLorme) via ruby-core
2024-12-18  6:11 ` [ruby-core:120289] " matz (Yukihiro Matsumoto) via ruby-core
2024-12-18 17:28 ` [ruby-core:120301] " Dan0042 (Daniel DeLorme) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).