2008-04-15
Closures in ruby
class File
def File.grep(fileName, pattern)
IO.foreach(fileName) do |line|
if md = pattern.match(line)
yield md;
end
end
end
end
File.grep("foo.txt", /total: (\d+)/) { |md| puts md[1]; }
延续是台时光机,闭包只是对旧时光的怀念







评论排行榜