Ruby snippet: Remover todos los caracteres no ASCII 127 de un string

Lo encontré hace tiempo en Stackoverflow, pero no encuentro el link :\

class String

  # remove all non 127-ASCII chars
  def remove_non_ascii
    n=self.split("")
    self.slice!(0..self.size)
    n.each { |b|
      if b[0].to_i< 33 || b[0].to_i>127 then
        self.concat('')
      else
         self.concat(b)
      end
    }
    self.to_s
  end
  
end

Ejemplo

puts "Ñandú" #and

Gist

Advertisement

Acerca de Esteban Martini

Software developer
Esta entrada fue publicada en Ruby y etiquetada , , . Guarda el enlace permanente.

Deja un comentario

Fill in your details below or click an icon to log in:

Logo de WordPress.com

You are commenting using your WordPress.com account. Log Out / Cambiar )

Twitter picture

You are commenting using your Twitter account. Log Out / Cambiar )

Facebook photo

You are commenting using your Facebook account. Log Out / Cambiar )

Connecting to %s