Franks original question — and his example implementation — was a bit confusing. His question, coupled with code, was:
any suggestions on preserving the order of an array when using threads? i want it to print back the array in the order it was added in.
Frank wanted the output in order, meaning he wanted to print after the previously spawned thread had printed. After some delirium (I have the flu), I whopped up an example using #inject
:
Now that is nice; it works! Frank was happy, and quickly disappeared with a […] thanks! im going to try it in a few mins […]
.
He’s very excited. He has now written a script that first reads a list of ip addresses from the user, and then goes on to visit each address and running a script on it using net/ssh. It looked like this:
Now, this is quite different from what Frank originally asked! This is a script that will read a list of hosts from a file, and then visit each host executing the command within ssh.exec!
on each host. In the end, it will print the results.
Me and Frank discussed his code and why he was doing this for a few minutes. He explained to me that he works as a system administrator, and does not really consider himself a programmer. Frank is too hard on himself.
Either way, I eventually began to see the code as a puzzle: could I improve the code, making it nicer and useful in other cases — and possibly even help Frank even more?
The answer to that is yes! Ten minutes later, I showed Frank what I came up with:
Executing this code like so ruby script.rb < instructions.txt
, with instructions.txt containing this:
superman 127.0.0.1 Kim whoami
database 127.0.0.1 Kim sleep 1 && echo "Database says Hello!"
localhost 127.0.0.1 Kim date
Will give this output:
[superman]
Kim
[database]
Database says Hello!
[localhost]
Thu Mar 10 23:31:59 CET 2011
Frank was happy, I was happy. Me and Frank talked for another twenty minutes, and then parted ways. Goodbye Frank, it was nice talking to you today!
PS: Here’s the gist of it: https://gist.github.com/865129