From: ara.t.howard@... Date: 2007-03-03T12:42:00+09:00 Subject: Re: String.each On Sat, 3 Mar 2007, Yannick Grams wrote: > Hello all! > > I'm fairly new to Ruby, and I'm trying to write a program that looks at > each character of a string and then processes it using a block. I've > been using: > > String.each do > #block > end > > but something isn't working. I'm sure that there is a simple answer, but > I'm not that experienced with the language. If someone could please help > me out, I'd greatly appreciate it. > > -- > Posted via http://www.ruby-forum.com/. harp: ~> ri String#each ------------------------------------------------------------ String#each str.each(separator=$/) {|substr| block } => str str.each_line(separator=$/) {|substr| block } => str ------------------------------------------------------------------------ Splits _str_ using the supplied parameter as the record separator (+$/+ by default), passing each substring in turn to the supplied block. If a zero-length record separator is supplied, the string is split on +\n+ characters, except that multiple successive newlines are appended together. print "Example one\n" "hello\nworld".each {|s| p s} print "Example two\n" "hello\nworld".each('l') {|s| p s} print "Example three\n" "hello\n\n\nworld".each('') {|s| p s} _produces:_ Example one "hello\n" "world" Example two "hel" "l" "o\nworl" "d" Example three "hello\n\n\n" "world" harp:~ > ruby -e' puts String.instance_methods.grep(/each/) ' each each_with_index each_line each_byte harp:~ > ruby -e' "foobar".each_byte{|b| p b} ' 102 111 111 98 97 114 harp:~ > ruby -e' "foobar".each_byte{|b| p b.chr} ' "f" "o" "o" "b" "a" "r" -a -- be kind whenever possible... it is always possible. - the dalai lama