Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't modify frozen String error using gsub and hash

I'm trying to create a ruby rspec with capybara and I'm getting this error when attempting to remove part of a string from a hash: can't modify frozen String

scenario "check doc" do
docs = {
  'A doc' => 'a.txt',
  'B doc' => 'b.txt',
  'C doc' => 'c.txt'
}
random_doc = docs.keys.sample
page.should have_css('.class', :text => 'Document ' + random_doc.gsub!(' doc') + ' was selected')

Any help would be appreciated!

like image 794
Donald C. Avatar asked Aug 31 '25 18:08

Donald C.


1 Answers

The key name is frozen so you can't modify it in place - just use gsub rather than gsub! so that it returns a modified copy of the string rather than trying to do inplace modification

like image 173
Thomas Walpole Avatar answered Sep 02 '25 08:09

Thomas Walpole