バックスラッシュを¥に変える

id:omochist:20060207#1139322402
僕ならどう書くかなぁ。ちょっと考えてみました。
実装1. ワンライナーぽく手抜きで。

#!/bin/sh
ruby -rjcode -Ke -pe '$_.tr! "\\", "¥"' $*

実装2. 普通にスクリプトとして。コマンドライン引数の解析も追加。とりあえず-oオプションの方だけ。

#!/usr/pkg/bin/ruby -Ke

require 'optparse'
require 'ostruct'

Version = '0.1a'

def open_output(filename)
  if filename
    open(filename, 'w') do |file|
      yield(file)
    end
  else
    yield($stdout)
  end
end

if $0 == __FILE__
  options = OpenStruct.new
  options.output_filename = nil
  OptionParser.new do |opt|
    opt.on('-o [filename]', 'set output file.') do |v|
      options.output_filename = v || 'hoge.out'
    end
    opt.parse!(ARGV)
  end
  
  open_output(options.output_filename) do |output|
    while line = ARGF.gets
      output.print line.gsub(/\\/, '¥')
    end
  end
end