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
とやっていましたが、ここではクラスを使いました。
- 作者: 高橋征義,後藤裕蔵,まつもとゆきひろ
- 出版社/メーカー: ソフトバンククリエイティブ
- 発売日: 2002/03
- メディア: 単行本
- 購入: 3人 クリック: 211回
- この商品を含むブログ (107件) を見る