ruby正则表达式打印匹配行

2015-10-15 0 876
ruby正则表达式打印匹配行
class Filetest
	attr_accessor :filename

	def initialize(filename)
		@filename = filename
	end

	def puts_file_line(word)
		File.open(@filename, "r") do |file|
			while line = file.gets
				if line =~ /#{word}/
					p line.chomp
				end
			end
		end
	end

end

file = Filetest.new("test")

file.puts_file_line("hello")

遇见资源网 ruby ruby正则表达式打印匹配行 http://www.ox520.com/16483.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务