Skip to content

Instantly share code, notes, and snippets.

@elia
Created March 2, 2011 10:08
Show Gist options
  • Save elia/850728 to your computer and use it in GitHub Desktop.
Save elia/850728 to your computer and use it in GitHub Desktop.
module SocketWithStats
attr_reader :read_bytes
def read *args
@read_bytes ||= 0.bytes
got = super
@read_bytes += args.first
return got
end
end
TCPSocket.open(host,port) do |socket|
socket.extend SocketWithStats
Thread.new { loop {puts socket.read_bytes} }
socket.gets
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment