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")