Ajmal
Ajmal

Reputation: 51

File open - with open socket - ruby - reading file continuously

require 'open-uri'
file_contents = open('local-file.txt') { |f| f.read }

taking the file open method forward.. how can we open and read a local file - with a continuously live or changing data???

Some thing similar to live feed.. etc - other than reading the file say every 30 seconds.. is there a way to keep a file connection open - so that we can log all changes happening...

Upvotes: 0

Views: 360

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118261

Read IO::open documentation :

With no associated block, IO.open is a synonym for ::new. If the optional code block is given, it will be passed io as an argument, and the IO object will automatically be closed when the block terminates. In this instance, ::open returns the value of the block.

without block

file = File.open('doc.txt')
file.closed? # => false

with block

file = File.open('doc.txt') {|f| f }
file.closed? # => true

is there a way to keep a file connection open - so that we can log all changes happening...

Then I can say don't use block with the File::open method.

Upvotes: 1

Related Questions