From: dblack@... Date: 2006-12-01T04:13:04+09:00 Subject: Re: simple question, looping through each character in a string Hi -- On Fri, 1 Dec 2006, 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) ) > } There's an each_byte iterator. It gives you ASCII values, so you have to convert them: word = "picture" word.each_byte {|b| puts b.chr } You can also do: word.split(//).each {|char| puts char } (Note that this is an area of Ruby that's undergoing a lot of changes in the transition from 1.8 to 1.9/2.0.) David -- David A. Black | dblack@wobblini.net Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org