Forum: Ruby-core [Backport93 - Backport #7354][Open] String#gsub not working as expected

Posted by moonr0ck (Ilya Ostrovskiy) (Guest)
on 2012-11-15 07:14
(Received via mailing list)
Issue #7354 has been reported by moonr0ck (Ilya Ostrovskiy).

----------------------------------------
Backport #7354: String#gsub not working as expected
https://bugs.ruby-lang.org/issues/7354

Author: moonr0ck (Ilya Ostrovskiy)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin

Windows 7, with Ruby 1.9.3p327, originally found in 1.9.3.p194
 irb(main):001:0> filename = "test.txt"
 => "test.txt"
 irb(main):002:0> suffix = "-suffix"
 => "-suffix"
 irb(main):003:0> filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix}#{$2}")
 => "-suffix"
 irb(main):004:0> # ^ that doesn't look right
 irb(main):005:0* # now if i add some spaces to the replacement string
 irb(main):006:0* # it will work like it should (with the spaces)
 irb(main):007:0* filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix} #{$2}")
 => "test-suffix .txt"
 irb(main):008:0> # and if i run the original it will work fine now
 irb(main):009:0* filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix}#{$2}")
 => "test-suffix.txt"
 irb(main):010:0> # Running Windows 7, installed using RubyInstaller.org
 irb(main):011:0* "#{RUBY_VERSION}p#{RUBY_PATCHLEVEL} 
(#{RUBY_RELEASE_DATE})"
 => "1.9.3p327 (2012-11-10)"

Arch Linux with Ruby 1.9.3.p286
 irb(main):001:0> filename = "test.txt"
 => "test.txt"
 irb(main):002:0> suffix = "-suffix"
 => "-suffix"
 irb(main):003:0> filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix}#{$2}")
 => "-suffix"
 irb(main):004:0> # ^ that's not right
 irb(main):005:0* # adding spaces works just like in windows
 irb(main):006:0* filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix} #{$2}")
 => "test-suffix .txt"
 irb(main):007:0> # and removing the space gives the expected result now
 irb(main):008:0* filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix}#{$2}")
 => "test-suffix.txt"
 irb(main):009:0> # Reproduced on Arch Linux
 irb(main):010:0* "#{RUBY_VERSION}p#{RUBY_PATCHLEVEL} 
(#{RUBY_RELEASE_DATE})"
 => "1.9.3p286 (2012-10-12)"

=end
Posted by moonr0ck (Ilya Ostrovskiy) (Guest)
on 2012-11-15 07:25
(Received via mailing list)
Issue #7354 has been updated by moonr0ck (Ilya Ostrovskiy).


It appears the spaces have no effect, and simply re-running the gsub is 
sufficient to get the expected result.

    irb(main):001:0> filename = "test.txt"
    => "test.txt"
    irb(main):002:0> suffix = "-suffix"
    => "-suffix"
    irb(main):003:0> filename.gsub(/^(.+)(\..+)$/, 
"#{$1}#{suffix}#{$2}")
    => "-suffix"
    irb(main):004:0> filename.gsub(/^(.+)(\..+)$/, 
"#{$1}#{suffix}#{$2}")
    => "test-suffix.txt"
----------------------------------------
Backport #7354: String#gsub not working as expected
https://bugs.ruby-lang.org/issues/7354#change-32918

Author: moonr0ck (Ilya Ostrovskiy)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin

Windows 7, with Ruby 1.9.3p327, originally found in 1.9.3.p194
 irb(main):001:0> filename = "test.txt"
 => "test.txt"
 irb(main):002:0> suffix = "-suffix"
 => "-suffix"
 irb(main):003:0> filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix}#{$2}")
 => "-suffix"
 irb(main):004:0> # ^ that doesn't look right
 irb(main):005:0* # now if i add some spaces to the replacement string
 irb(main):006:0* # it will work like it should (with the spaces)
 irb(main):007:0* filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix} #{$2}")
 => "test-suffix .txt"
 irb(main):008:0> # and if i run the original it will work fine now
 irb(main):009:0* filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix}#{$2}")
 => "test-suffix.txt"
 irb(main):010:0> # Running Windows 7, installed using RubyInstaller.org
 irb(main):011:0* "#{RUBY_VERSION}p#{RUBY_PATCHLEVEL} 
(#{RUBY_RELEASE_DATE})"
 => "1.9.3p327 (2012-11-10)"

Arch Linux with Ruby 1.9.3.p286
 irb(main):001:0> filename = "test.txt"
 => "test.txt"
 irb(main):002:0> suffix = "-suffix"
 => "-suffix"
 irb(main):003:0> filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix}#{$2}")
 => "-suffix"
 irb(main):004:0> # ^ that's not right
 irb(main):005:0* # adding spaces works just like in windows
 irb(main):006:0* filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix} #{$2}")
 => "test-suffix .txt"
 irb(main):007:0> # and removing the space gives the expected result now
 irb(main):008:0* filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix}#{$2}")
 => "test-suffix.txt"
 irb(main):009:0> # Reproduced on Arch Linux
 irb(main):010:0* "#{RUBY_VERSION}p#{RUBY_PATCHLEVEL} 
(#{RUBY_RELEASE_DATE})"
 => "1.9.3p286 (2012-10-12)"

=end
Posted by naruse (Yui NARUSE) (Guest)
on 2012-11-15 07:28
(Received via mailing list)
Issue #7354 has been updated by naruse (Yui NARUSE).

Status changed from Open to Rejected

Embeded $1 is evaled before gsub runs.

You should write it as
  filename.gsub(/^(.+)(\..+)$/){"#{$1}#{suffix}#{$2}"}
or
  filename.gsub(/^(.+)(\..+)$/, "\\1#{suffix}\\2")
----------------------------------------
Backport #7354: String#gsub not working as expected
https://bugs.ruby-lang.org/issues/7354#change-32919

Author: moonr0ck (Ilya Ostrovskiy)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


=begin

Windows 7, with Ruby 1.9.3p327, originally found in 1.9.3.p194
 irb(main):001:0> filename = "test.txt"
 => "test.txt"
 irb(main):002:0> suffix = "-suffix"
 => "-suffix"
 irb(main):003:0> filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix}#{$2}")
 => "-suffix"
 irb(main):004:0> # ^ that doesn't look right
 irb(main):005:0* # now if i add some spaces to the replacement string
 irb(main):006:0* # it will work like it should (with the spaces)
 irb(main):007:0* filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix} #{$2}")
 => "test-suffix .txt"
 irb(main):008:0> # and if i run the original it will work fine now
 irb(main):009:0* filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix}#{$2}")
 => "test-suffix.txt"
 irb(main):010:0> # Running Windows 7, installed using RubyInstaller.org
 irb(main):011:0* "#{RUBY_VERSION}p#{RUBY_PATCHLEVEL} 
(#{RUBY_RELEASE_DATE})"
 => "1.9.3p327 (2012-11-10)"

Arch Linux with Ruby 1.9.3.p286
 irb(main):001:0> filename = "test.txt"
 => "test.txt"
 irb(main):002:0> suffix = "-suffix"
 => "-suffix"
 irb(main):003:0> filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix}#{$2}")
 => "-suffix"
 irb(main):004:0> # ^ that's not right
 irb(main):005:0* # adding spaces works just like in windows
 irb(main):006:0* filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix} #{$2}")
 => "test-suffix .txt"
 irb(main):007:0> # and removing the space gives the expected result now
 irb(main):008:0* filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix}#{$2}")
 => "test-suffix.txt"
 irb(main):009:0> # Reproduced on Arch Linux
 irb(main):010:0* "#{RUBY_VERSION}p#{RUBY_PATCHLEVEL} 
(#{RUBY_RELEASE_DATE})"
 => "1.9.3p286 (2012-10-12)"

=end
Posted by moonr0ck (Ilya Ostrovskiy) (Guest)
on 2012-11-15 07:44
(Received via mailing list)
Issue #7354 has been updated by moonr0ck (Ilya Ostrovskiy).


Thank you! :) I had a feeling that perhaps it wasn't a bug and I was 
just doing something wrong.
----------------------------------------
Backport #7354: String#gsub not working as expected
https://bugs.ruby-lang.org/issues/7354#change-32920

Author: moonr0ck (Ilya Ostrovskiy)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


=begin

Windows 7, with Ruby 1.9.3p327, originally found in 1.9.3.p194
 irb(main):001:0> filename = "test.txt"
 => "test.txt"
 irb(main):002:0> suffix = "-suffix"
 => "-suffix"
 irb(main):003:0> filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix}#{$2}")
 => "-suffix"
 irb(main):004:0> # ^ that doesn't look right
 irb(main):005:0* # now if i add some spaces to the replacement string
 irb(main):006:0* # it will work like it should (with the spaces)
 irb(main):007:0* filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix} #{$2}")
 => "test-suffix .txt"
 irb(main):008:0> # and if i run the original it will work fine now
 irb(main):009:0* filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix}#{$2}")
 => "test-suffix.txt"
 irb(main):010:0> # Running Windows 7, installed using RubyInstaller.org
 irb(main):011:0* "#{RUBY_VERSION}p#{RUBY_PATCHLEVEL} 
(#{RUBY_RELEASE_DATE})"
 => "1.9.3p327 (2012-11-10)"

Arch Linux with Ruby 1.9.3.p286
 irb(main):001:0> filename = "test.txt"
 => "test.txt"
 irb(main):002:0> suffix = "-suffix"
 => "-suffix"
 irb(main):003:0> filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix}#{$2}")
 => "-suffix"
 irb(main):004:0> # ^ that's not right
 irb(main):005:0* # adding spaces works just like in windows
 irb(main):006:0* filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix} #{$2}")
 => "test-suffix .txt"
 irb(main):007:0> # and removing the space gives the expected result now
 irb(main):008:0* filename.gsub(/^(.+)(\..+)$/, "#{$1}#{suffix}#{$2}")
 => "test-suffix.txt"
 irb(main):009:0> # Reproduced on Arch Linux
 irb(main):010:0* "#{RUBY_VERSION}p#{RUBY_PATCHLEVEL} 
(#{RUBY_RELEASE_DATE})"
 => "1.9.3p286 (2012-10-12)"

=end
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.