readable, _, _ = IO.select(sockets)
为什么需要连个占位符?
This method will generate warnings that the family, port, and host variables are unused.
def get_ip(sock)
family, port, host, address = sock.peeraddr
address
end
Using underscores will stop the warnings, but lose the self-documenting nature of the code
def get_ip(sock)
_, _, _, address = sock.peeraddr
address
end
As of Ruby 2.0.0 we can get the best of both worlds by starting the variables with an _
def get_ip(sock)
_family, _port, _host, address = sock.peeraddr
address
end
不用定义函数,我指的
readable = (IO.select(sockets) || []).first
或者
readable = IO.select(sockets).to_a.first
其实不怕污染的话简单粗暴定义
def nil.first; nil end
就可以直接
readable = IO.select(sockets).first