Ripper
http://ujihisa.blogspot.com/2009/07/parsers-around-ruby.html の記事の最後にあったので、あんまり期待してないで調べはじめたら、これgemとかじゃなくて、1.9系からの標準添付ライブラリなのね。
ちょっとテンション高めで使ってみたらいけました!コメントも取れる!
もちろん環境はruby-1.9.2
require 'ripper' => true lines = <<EOS # AAAAAA def foo(*args) puts args.inspect end EOS #=> " # AAAAAA\n def foo(*args)\n puts args.inspect\n end\n" Ripper.tokenize lines #=> [" ", "# AAAAAA\n", " ", "def", " ", "foo", "(", "*", "args", ")", "\n", " ", "puts", " ", "args", ".", "inspect", "\n", " ", "end", "\n"] Ripper.lex lines #=> [[[1, 0], :on_sp, " "], [[1, 2], :on_comment, "# AAAAAA\n"], [[2, 0], :on_sp, " "], [[2, 2], :on_kw, "def"], [[2, 5], :on_sp, " "], [[2, 6], :on_ident, "foo"], [[2, 9], :on_lparen, "("], [[2, 10], :on_op, "*"], [[2, 11], :on_ident, "args"], [[2, 15], :on_rparen, ")"], [[2, 16], :on_ignored_nl, "\n"], [[3, 0], :on_sp, " "], [[3, 4], :on_ident, "puts"], [[3, 8], :on_sp, " "], [[3, 9], :on_ident, "args"], [[3, 13], :on_period, "."], [[3, 14], :on_ident, "inspect"], [[3, 21], :on_nl, "\n"], [[4, 0], :on_sp, " "], [[4, 2], :on_kw, "end"], [[4, 5], :on_nl, "\n"]]
すばらしいい!!