Logging Ruby - The Ruby alias for the forgetful scripter
Only Feature: No more scrolling through your terminal… Logs the output of a script to the script itself!
First, clone the repo on your local machine.
Create an alias for lruby like this:
alias lruby="ruby -r /path-to-your-lruby/lruby.rb"Check that the alias exists by calling which lruby in your terminal.
Once lruby is available, you can call your Ruby scripts with lruby instead of ruby.
Let’s take a look at ./examples/hello_world.rb.
cat ./examples/hello_world.rbIt is a very simple ruby script that writes “Hello world” and the current time using puts.
puts “Hello world! It is #{Time.now.strftime(‘%H:%m:%S’)}”
As expected when calling it with Ruby…
ruby ./examples/hello_world.rb…it will do exactly that.
Hello world! It is 13:04:26Now let’s use lruby to call the script:
lruby ./examples/hello_world.rbAgain, the output is the same:
Hello world! It is 13:05:04But when looking at the source file:
cat ./examples/hello_world.rbYou will realize that the output of the last call has been logged to the script itself.
puts "Hello world! It is #{Time.now.strftime('%H:%m:%S')}"
__END__
----- [2020-04-19T13:05:04+0200] RESULTS -----
Hello world! It is 13:05:04But don’t worry! It is still a valid Ruby file and can still be called with the normal Ruby interpreter:
ruby ./examples/hello_world.rbAnd will still just produce the expected output.
Hello world! It is 13:05:50