From: Robert Klemme Date: 2010-08-30T18:02:11+09:00 Subject: Re: Speed issues iterating over chars 2010/8/30 Martin Hansen : > Hello all, > > > This snip of code needs more speed. Ideas? What is the state of Ruby and > Inline C? > > > Cheers, > > Martin > > > > class Hash >  BASE_SOLEXA = 64 > >  # Soft masks sequence residues where the corresponding quality score >  # is below a given cutoff. >  def mask!(cutoff) >    if self.has_key? :SEQ and self.has_key? :SCORES >      seq    = self[:SEQ] >      scores = self[:SCORES] >      i      = 0 > >      scores.each_char do |score| >        seq[i] = seq[i].downcase if score.ord - BASE_SOLEXA <= cutoff >        i += 1 >      end > >      self[:SEQ] = seq >    end > >    self >  end > end What about using String#gsub!? # untested seq.gsub! /(?:#{(0..(BASE_SOLEXA+cutoff)).map{|ch|"\\u00%02x" % ch}.join("|")})+/ do |m| m.downcase! end You could speed up calculation of the regexp by placing this in the default block of a Hash, e.g. RXS = Hash.new {|h,cutoff| h[cutoff] = /(?:#{(0..(BASE_SOLEXA+cutoff)).map{|ch|"\\u00%02x" % ch}.join("|")})+/ and then seq.gsub! RXS[cutoff] do |m| m.downcase! end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/