From: Julian Leviston Date: 2011-08-10T09:33:42+09:00 Subject: Re: *WHY* does this not work? Or use each_eith_index which is a bit nicer rather than constructing a range. Blog: http://random8.zenunit.com/ Twitter: http://twitter.com/random8r Learn: http://sensei.zenunit.com/ New video up now at http://sensei.zenunit.com/ real fastcgi rails deploy process! Check it out now! On 10/08/2011, at 5:20 AM, "Darryl L. Pierce" wrote: > On Wed, Aug 10, 2011 at 04:11:05AM +0900, serialhex wrote: >> On Tue, Aug 9, 2011 at 3:05 PM, Darryl L. Pierce wrote: >> >>> On Wed, Aug 10, 2011 at 03:52:59AM +0900, serialhex wrote: >>>> ok, so code: >>>> >>>> str = 'foo' >>>> => "foo" >>>> str.each_char do |c| >>>> c = 'x' >>>> end >>>> => "foo" >>>> >>>> WHY does str == 'foo' still?? >>> >>> Because each_char is not destructive to the original string's value. It >>> moves through the string as an array of characters and then passes each >>> one _by value_ to the block supplied. So while you might be able to >>> locally alter the value for that character, you don't have access to the >>> original string itself in the block. >> >> so is there a way to do that? easily without jumping through hoops of >> fire?? > > You want to iterate through a string and change individual characters? > Why not just do: > > str = "farkle" > (0...str.size).each {|index| str[index] = 'x'} > > -- > Darryl L. Pierce > http://mcpierce.multiply.com/ > "What do you care what people think, Mr. Feynman?"