Mega Code Archive

 
Categories / Ruby / File Directory
 

Performing Random Access on Read-Once Input Streams

require 'socket' require 'stringio' sock = TCPSocket.open("www.example.com", 80) sock.write("GET /\n") file = StringIO.new(sock.read) file.read(10)                                   # => "<HTML>\r\n<H" file.rewind file.read(10)                                   # => "<HTML>\r\n<H" file.pos = 90 file.read(15)                                   # => " this web page "