behaviour change of String#gsub(pattern) {|m| ... } for ruby 1.9/ruby2?

D

David Garamond

String#gsub(pattern) {|m| ... }

It really would be nice to get match data in 'm', but this would surely
break _a lot_ of scripts. How about String#regsub, #regsub!, #resub,
#resub! ? What do others think?
 
Y

Yukihiro Matsumoto

Hi,

In message "behaviour change of String#gsub(pattern) {|m| ... } for ruby 1.9/ruby2?"

|String#gsub(pattern) {|m| ... }
|
|It really would be nice to get match data in 'm', but this would surely
|break _a lot_ of scripts. How about String#regsub, #regsub!, #resub,
|#resub! ? What do others think?

Sounds nice. The only reason for the current behavior is that sub
predates MatchData. But we have to define migration path.

matz.
 
F

Florian Gross

Yukihiro said:
Hi,
Moin!

|String#gsub(pattern) {|m| ... }
|
|It really would be nice to get match data in 'm', but this would surely
|break _a lot_ of scripts. How about String#regsub, #regsub!, #resub,
|#resub! ? What do others think?

Sounds nice. The only reason for the current behavior is that sub
predates MatchData. But we have to define migration path.

I like this, because matz told me I shouldn't be using $1 in the
gsub-block and there's no way to not do so right now. :)

Add a MatchData#to_str and it should work for most cases. We could give
a warning for cases where scripts would still be broken by this chance.

Regards,
Florian Gross
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top