Matthew Moss wrote:
> Nice and easy one this week. Your task is to write a Ruby script that
> reports the date and time of your last reboot, making use of  the
> `uptime` command.

Why use `uptime` when there's `who -b`?

Here is my solution. It should run out of the box (i.e. no gems needed) 
on all major platforms.

---
require 'time'

methods = [
  lambda { # Windows
    require 'Win32API'
    getTickCount = Win32API.new("kernel32", "GetTickCount", nil, 'L')
    Time.now - getTickCount.call() / 1000.0
  },
  lambda { # *BSD, including Mac OS
    Time.at(`sysctl -b kern.boottime 2>/dev/null`.unpack('L').first)
  },
  lambda { # Some other form of *nix
    Time.parse(`who -b 2>/dev/null`)
  }
]

begin
  unless methods.empty?
    boot_time = methods.shift.call
  else
    puts "Unable to determine time of last reboot.  Sorry!"
    exit!(1)
  end
rescue Exception
  retry
end

puts "Last reboot: #{boot_time.asctime}"
---

Regards,
Matthias
-- 
Posted via http://www.ruby-forum.com/.