Chinaunix

标题: 请大家帮忙看看哪里错了? [打印本页]

作者: Iinvincible    时间: 2013-09-11 11:35
标题: 请大家帮忙看看哪里错了?
filename = ARGV.first
txt = File.open(filename)
for line in txt.read()
   if line = ~/put/
   puts $_
   else
   puts "NOT match"
   puts $_
   end
end

打印结果:
NOT match
nil
NOT match
nil
NOT match
nil

就是想读文件的每一行,然后打印含有put字段的行,可是总是nil

作者: shijiang1130    时间: 2013-09-11 17:16

作者: bikong0411    时间: 2013-09-11 21:56
f=File.open('/etc/passwd','r')
while line = f.readline
    print line if line =~ /root/
end
作者: Sevk    时间: 2013-09-12 21:48
提示: 作者被禁止或删除 内容自动屏蔽
作者: Iinvincible    时间: 2013-09-16 11:33
谢谢各位!




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2