From: Adam Prescott Date: 2011-08-10T21:02:52+09:00 Subject: Re: *WHY* does this not work? --20cf307f365ea6221904aa2575b1 Content-Type: text/plain; charset=UTF-8 On Tue, Aug 9, 2011 at 8:27 PM, John W Higgins wrote: > str.length.times do |i| > str[i] = 'x' > end > As an alternative to this (although I'd just use #gsub!), if the character doesn't change then you might as well do: str.replace("x" * str.length) To satisfy my curiosity, a crude benchmark: $ cat str_replace_benchmark.rb require "benchmark" puts Benchmark.realtime { str = "hello, world!"; 10_000.times { str.replace("x" * str.length) } } puts Benchmark.realtime { str = "hello, world!"; 10_000.times { str.gsub!(/./, "x") } } $ ruby str_replace_benchmark.rb 0.007066011428833008 0.10264110565185547 --20cf307f365ea6221904aa2575b1--