each_char

A Ruby script to remove binary (garbage) characters from a text file

Problem: You have a file that should be a plain text file, but for some reason it has a bunch of non-printable binary characters (also known as garbage characters) in it, and you'd like a Ruby script that can create a clean version of the file.

Solution: I've demonstrated how to do this in another blog post by using the Unix tr command, but in case you'd like a Ruby script to clean up a file like this, I thought I'd write up a quick program and share it here.

Ruby - How to convert characters to ASCII decimal (byte) values

Problem: You have a character, or a string of characters, and you want to use a Ruby script to convert each character to its ASCII decimal (byte) value.

Solution

I just ran into this problem while working on a script to remove binary/garbage characters from a Unix text file. In short, all you have to do to convert a character in Ruby to its equivalent decimal ASCII code is use the ? operator in front of it, like this:

How to loop through each character in a Ruby String

Ruby character/string FAQ: How can I loop through each character in a Ruby String, and perform some operation on each character?

I'm currently using Ruby 1.8.6, and you can use the Ruby each_char method if you'll first require the jcode module. To be clear, this code will not work in Ruby 1.8.6:

a = 'hello, world'
a.each_char { |c|
  puts c
}

In fact it results in the following error: