我在机器上运行了一个很长的 ruby 脚本,我想通过将其划分为小函数并将套接字作为参数发送来重新组织它:
#!/usr/bin/env ruby
def prepare data, my_socket
# some calculations
my_socket.write data
end
# execution starts here
tcpsocket = TCPSocket.open host, port
data = "xxx"
prepare(data, tcpsocket)
不幸的是我无法测试。
编辑:既然我明白我不应该这样做(我稍后会阅读这个主题),这就是我所做的:
#!/usr/bin/env ruby
def prepare data
my_array = []
# some calculations
my_array << data
end
# execution starts here
tcpsocket = TCPSocket.open host, port
data = "xxx"
my_array = prepare data
my_array.each do |m|
my_socket.write m
end