Performing Random Access on Read-Once Input Streams : StringIO « File Directory « Ruby






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 "

 








Related examples in the same category

1.Pretending a String is a File
2.Write to a string
3.YAML.dump
4.Find the fifth byte
5.push data to string
6.Redirecting Standard Input or Output