Skip to content

Add modification check on String#chomp! method with default separator#5041

Merged
enebo merged 1 commit intojruby:jruby-9.1from
nomadium:raise-error-on-changes-to-frozen-empty-string
Feb 13, 2018
Merged

Add modification check on String#chomp! method with default separator#5041
enebo merged 1 commit intojruby:jruby-9.1from
nomadium:raise-error-on-changes-to-frozen-empty-string

Conversation

@nomadium
Copy link
Contributor

When working on #5039 I noticed one of String tests on #chomp! method was not passing:

s = S("").freeze
assert_raise_with_message(FrozenError, /frozen/) {s.chomp!}

https://github.com/jruby/jruby/blob/ruby-2.5/test/mri/ruby/test_string.rb#L445

After looking closely, I learned that MRI folks introduced this test on ruby/ruby@80aa1e6.

I'm proposing this change since this is a difference between MRI and JRuby.

Behavior before applying this change:

miguel@alice:~/jruby/upstream/jruby$ PATH=$PWD/bin:$PATH jirb
irb(main):001:0> s = String.new("").freeze
=> ""
irb(main):002:0> s.chomp!
=> nil
irb(main):003:0>

Behavior after applying this change:

miguel@alice:~/jruby/upstream/jruby$ PATH=$PWD/bin:$PATH jirb
irb(main):001:0> s = String.new("").freeze
=> ""
irb(main):002:0> s.chomp!
RuntimeError: can't modify frozen String
        from org/jruby/RubyString.java:4286:in `chomp!'
        from (irb):2:in `<eval>'
        from org/jruby/RubyKernel.java:995:in `eval'
        from org/jruby/RubyKernel.java:1316:in `loop'
        from org/jruby/RubyKernel.java:1138:in `catch'
        from org/jruby/RubyKernel.java:1138:in `catch'
        from /home/miguel/jruby/upstream/jruby/bin/jirb:13:in `<main>'
irb(main):003:0> 

MRI behavior:

miguel@alice:~/jruby/upstream/jruby$ irb2.5
irb(main):001:0> s = String.new("").freeze
=> ""
irb(main):002:0> s.chomp!
Traceback (most recent call last):
        3: from /usr/bin/irb2.5:11:in `<main>'
        2: from (irb):2
        1: from (irb):2:in `chomp!'
FrozenError (can't modify frozen String)
irb(main):003:0> 

This change should be applied to master as well.

@enebo enebo added this to the JRuby 9.1.16.0 milestone Feb 13, 2018
@enebo enebo merged commit ba92c73 into jruby:jruby-9.1 Feb 13, 2018
@nomadium nomadium deleted the raise-error-on-changes-to-frozen-empty-string branch February 13, 2018 21:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants