Skip to content

Instantly share code, notes, and snippets.

@tune
Created May 1, 2010 15:19
Show Gist options
  • Save tune/386410 to your computer and use it in GitHub Desktop.
Save tune/386410 to your computer and use it in GitHub Desktop.
Add/Remove BOM
#!/usr/bin/ruby
require "optparse"
mode = :help
opt = OptionParser.new
opt.on("-a", "Add BOM"){|v| mode = :add}
opt.on("-d", "Delete BOM"){|v| mode = :delete}
opt.parse!(ARGV)
case mode
when :add
STDOUT.binmode
lines = readlines
unless lines[0] =~ /^\M-o\M-;\M-?/ then
print "\xEF\xBB\xBF"
end
print lines
when :delete
STDOUT.binmode
lines = readlines
lines[0].sub!(/^\xEF\xBB\xBF/, '')
print lines
when :help
STDERR.puts opt.help
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment