# \`concat': can't modify frozen string in benchmark.rb

**URL:** <https://www.ruby-forum.com/t/concat-cant-modify-frozen-string-in-benchmark-rb/105558>\
**Category:** Ruby\
**Created:** [May 31, 2007, 3:47am UTC](https://www.ruby-forum.com/t/concat-cant-modify-frozen-string-in-benchmark-rb/105558 "2007-05-31T03:47:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeSSSSaS\_Botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@PeSSSSaS\_Botp](https://www.ruby-forum.com/u/PeSSSSaS_Botp)\
**Post date:** [May 31, 2007, 3:47am UTC](https://www.ruby-forum.com/t/concat-cant-modify-frozen-string-in-benchmark-rb/105558/1 "2007-05-31T03:47:01Z")

</div>

Hi All,

C:\family\ruby\>cat test.rb  
def f(command\_here)  
`#{command_here}`  
end  
a = { “Local copy”=\>“copy test.txt test.txt2” }  
require ‘benchmark’  
Benchmark.bmbm do |x|  
a.each do |k,v|  
x.report(k) { f v }  
end  
end

C:\family\ruby\>ruby test.rb  
c:/ruby/lib/ruby/1.8/benchmark.rb:334:in `concat': can't modify frozen string (T ypeError) from c:/ruby/lib/ruby/1.8/benchmark.rb:334:in`report’  
from test.rb:12  
from test.rb:11:in `each' from test.rb:11 from c:/ruby/lib/ruby/1.8/benchmark.rb:250:in`bmbm’  
from test.rb:10  
C:\family\ruby\>

if i comment out line 334 in benchmark.rb like so,

```
 #label.concat ' '

```

It works.  
It also works for the ff cases  
x.report(k) { f v }  
x.report() { f v }  
x.report("") { f v }  
x.report(“test”) { f v }

What is the relevance of line 334 in benchmark.rb?  
If the line is important, how can I fix my program so I can pass a var  
in x.report?

thank you and kind regards -botp

---

<div class="post-metadata">

**Author:** ![PeSSSSaS\_Botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@PeSSSSaS\_Botp](https://www.ruby-forum.com/u/PeSSSSaS_Botp)\
**Post date:** [May 31, 2007, 3:58am UTC](https://www.ruby-forum.com/t/concat-cant-modify-frozen-string-in-benchmark-rb/105558/2 "2007-05-31T03:58:02Z")

</div>

Peña wrote:

> ```
> x.report(k) { f v }
> from c:/ruby/lib/ruby/1.8/benchmark.rb:250:in `bmbm'
> 
> ```
> 
> x.report() { f v }  
> x.report("") { f v }  
> x.report(“test”) { f v }
> 
> What is the relevance of line 334 in benchmark.rb?  
> If the line is important, how can I fix my program so I can pass a var in x.report?
> 
> thank you and kind regards -botp

A quick workaround would be to use k.dup:

```
   x.report(k.dup) { f v }

```

Apparently, benchmark is being bad and munging its inputs (and falling  
down when one of them happens to be frozen because it is also a hash  
key).

---

<div class="post-metadata">

**Author:** ![PeSSSSaS\_Botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@PeSSSSaS\_Botp](https://www.ruby-forum.com/u/PeSSSSaS_Botp)\
**Post date:** [May 31, 2007, 4:39am UTC](https://www.ruby-forum.com/t/concat-cant-modify-frozen-string-in-benchmark-rb/105558/3 "2007-05-31T04:39:06Z")

</div>

From: Joel VanderWerf [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] :

# A quick workaround would be to use k.dup:

# 

# x.report(k.dup) { f v }

brilliant insight. I never even tested that, thinking k.dup would just  
be another var/object. And besides, it does not even look \*right 🙂

# Apparently, benchmark is being bad and munging its inputs

# (and falling

# down when one of them happens to be frozen because it is also

# a hash key).

it is really weird.  
thank you and kind regards -botp
