What does TCPSocket#each iterate over in ruby? -


i'm not familiar ruby, wasn't able find documentation method.

when calling each on tcpsocket object, this

require "socket"  srv = tcpserver.new("localhost", 7887) skt = srv.accept skt.each {|arg| p arg} 

does block called once per tcp packet, once per line (after each '\n' char), once per string (after after each nul/eof), or different entirely?

tl;dr tcpsocket.each iterate each newline delimited \n string receives.

more details:

a tcpsocket basicsocket powder sugar on top. , basicsocket child of io class. io class stream of data; thus, iterable. , that where can find how each defined tcpsocket.

fire irb console , enter line of code $stdin socket see how each behaves. both inherit io. here example of happens:

irb(main):011:0> $stdin.each {|arg| p arg + "."} hello "hello\n." 

but directly answer question, block called once per \n character. if client sending data 1 character @ time block not going executed until sees \n.

here quick sample client show this:

irb(main):001:0> require 'socket' => true irb(main):002:0> s = tcpsocket.open("localhost", 7887) => #<tcpsocket:fd 9> irb(main):003:0> s.puts "hello" => nil irb(main):007:0> s.write "hi" => 2 irb(main):008:0> s.write ", nice meet you" => 18 irb(main):009:0> s.write "\n" => 1 

and here server printed out:

"hello\n" "hi, nice meet you\n" # note: did not print until sent "\n" 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -