Ruby で grep

#!/usr/local/bin/ruby

class Grep
  def initialize(string)
    @string = string || ''
  end

  def grep(pattern)
    @string if pattern.match(@string)
  end
end 

pattern, file = Regexp.new(ARGV[0]), ARGV[1]

File.open(file).each do |line|
  g = Grep.new(line)
  g.grep(pattern).display
end

たのしい Ruby では

file = open(filename)
while text = file.gets do
  print text
end
file.close

とやっていましたが、ここではクラスを使いました。