I am writing a script to get the process image in unix through ssh, then
insert the record to database. As I am a new ruby user, I think my
method is quite stupid and I would like to know if you can make my code
prettier. Thanks.

Below is the ps image in my unix server (command : ps auxww)
USER         PID %CPU %MEM   SZ  RSS    TTY STAT    STIME  TIME COMMAND
\n
testrrs1 2429158  0.0  0.0 15588 15624      - A    13:27:12  0:00 java
lis.rpt.PostPrtMain /appl/lis/home2/testrrs1/config/PPRT_3.att \n
testlib  2425018  0.0  0.0 3052 4772      - A    May 29  0:00
/appl/lis/home/lib/test/work/rls_server -i/appl/lis/home/lib/test/w \n


====start of script===
require "rubygems"
$LOADED_FEATURES[$LOADED_FEATURES.length] =
'net/ssh/authentication/pageant.rb'
require 'net/ssh'
require "active_record"
$config = YAML.load_file(File.join(File.dirname(__FILE__),
'database.yml'))

ssh=Net::SSH.start("hostname", "account", :port => 12345, :paranoid =>
false, :auth_methods => ["publickey"], :passphrase => "testing", :keys
=> ["C:/testing/id_rsa"])

ps=ssh.exec!("ps auxww | head -30")
ssh.close

ps=ps.to_a
for idx in (0...ps.length)
   next if idx==0   #skip the header
   ps_user=ps[idx].split[0]
   ps_pid=ps[idx].split[1]
   ps_cpu=ps[idx].split[2]
   ps_mem=ps[idx].split[3]
   ps_sz=ps[idx].split[4]
   ps_rss=ps[idx].split[5]
   ps_tty=ps[idx].split[6]
   ps_stat=ps[idx].split[7]

   # The STIME field may equal to May 29 or 13:27:12, and the COMMAND
has unknown number of space.
   if (ps[idx].split[8].include?(":"))
      ps_stime=ps[idx].split[8]
      ps_cmd=ps[idx].split[9]+" "+ps[idx].split[10]+" "+ ... +
ps[idx].split[ps[idx].length]
   else
      ps_stime=ps[idx].split[8]+" "+ps[idx].split[9]
      ps_cmd=ps[idx].split[10]+" "+ps[idx].split[11]+" "+ ... +
ps[idx].split[ps[idx].length]
   end

   MyTable.create(:ps_user => ps_user, :ps_pid => ps_pid, ...SKIP...
:ps_cmd => ps_cmd)

end

====end of script===

Thank you very much for your kind assistance.
-- 
Posted via http://www.ruby-forum.com/.