3 ms·
Not sure if you are familiar with Enumerator class in Ruby, it does pretty much what generators in PHP are about: require 'enumerator' def each_line_from_
by jablan 14y ago
Not sure if you are familiar with Enumerator class in Ruby, it does pretty much what generators in PHP are about:
require 'enumerator'
def each_line_from_file file_name
Enumerator.new do |yielder|
File.open(file_name) do |file|
while line = file.gets
yielder << line
## or, alternatively:
# yielder.yield(line)
end
end
end
end
each_line_from_file('test.txt').each do |l|
puts l
end