Simple
puts Readline::HISTORY.to_a
Found answer here
“Time is stored internally as the number of seconds with fraction since the Epoch, January 1, 1970 00:00 UTC.”
>>require 'time'
>>Time.parse("January 1, 1970 00:00 UTC").to_i
=> 0
As expected, but nice to verify.
For the reverse, be sure to use Time.at rather than Time.new
>> Time.at(0).to_i => 0 >> Time.new(0).to_i => -62167201200
def main#option parsing and execution code hereendif __FILE__ == $0main()end
This way the main function will only be automatically called if the script is being executed on the command line.
And in irb, you can call your functions and classes as you see fit for testing without triggering your whole script to run.
Note that this is similar to the python __main__ test if you are coming from a python background.
#!/usr/bin/env ruby#!/usr/bin/ruby
require 'script-name'load 'script-name'exit 0exit 27$stderr.puts "error: problem ...."
Once in a while you need to control an interactive command line tool. I kept getting a block when trying to read from a ruby IO popen pipe that was waiting for input. Here is my simple solution/workaround:
sh_process = IO.popen('sh > out.log', 'w')
f = File.open("out.log", "r")
sh_process.puts("ls")
f.read
sh_process.puts("uptime")
f.read
...
f.close
Basically, just create a temp file and read from that. A little hackish, but it works.
Ruby’s built-in telnet capability has been extremely useful for scripting automated RAID changes like LUN masking, etc.
It is super simple. Connect, login, issue commands, read and parse results.
The example code I used is here.
This is telnet, so don’t do this if eavesdropping is a concern.