From: matt@... (matt neuburg) Date: 2006-12-01T05:15:09+09:00 Subject: Re: simple question, looping through each character in a string warhero wrote: > how can I accomplish something like this in ruby: > > pseudo code: > > word = "picture" > for( i = 0; i < word.length; i++ ) > { > puts( word.substr(i,1) ) > } > > > ruby? > > I've tried something like: > > word = "picture" > word.each { |char| puts char } > > but that doesn't do what I am wanting. it ends up just putting the > entire word "picture" > > I've also tried: > > word = "picture" > for i in 0..word.length - 1 > puts word[i] > end > > that just puts out ascii numbers.. > > I thought strings could be access like arrays? > word = "picture" > puts word[0] -> 116 What I do is this: the_string.scan(/./).each do |char| However, do note that, as others have said, in Ruby 1.9 this will no longer be necessary (though it will still work). m. -- matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/ Tiger - http://www.takecontrolbooks.com/tiger-customizing.html AppleScript - http://www.amazon.com/gp/product/0596102119 Read TidBITS! It's free and smart. http://www.tidbits.com