> In a ruby program I'm writing, I want to parse a string in reverse > and feed each of its individual characters through a state machine. > A "hammer and tongs" solution looks like this: > string.reverse.split(//).each { > |ch| > feedIntoStateMachine(ch) > } > However, this seems rather inefficient. Does anyone know of a faster > algorithm for this in ruby? Have you tried benchmark? require 'benchmark' include Benchmark str = "Hammer and tongs" bm(7) do |x| x.report("1") { 100000.times { str.reverse.split(//).each { |ch| ch } } } x.report("2") { 100000.times { str.reverse.split('').each { |ch| ch } } } x.report("3") { 100000.times { str.reverse.each_byte { |ch| ch.chr } } } end user system total real 1 4.094000 0.000000 4.094000 ( 4.109000) 2 4.125000 0.015000 4.140000 ( 4.141000) 3 2.375000 0.000000 2.375000 ( 2.391000)