
Ruby snippet for Macintosh battery status ![]()
If you are using Mac OS X on a laptop, the following Ruby code may be useful:
ioreg_data = IO.popen("ioreg -w0 -l | grep Capacity").readlines()
current = ioreg_data[0].scan(/[0-9]+/)[0].to_f
max = ioreg_data[3].scan(/[0-9]+/)[0].to_f
percent = (current / max) * 100
puts percent.to_i.to_s
It does one very simple task - loads from ioreg the battery statistics for your Mac, and just pulls the relevant numbers out using regular expressions and spits it back to you.