#!/usr/bin/env ruby
#
# rb - a tiny inline Ruby script that can be used during the shell session.
#
# Examples
#
# Extract docker images from running containers
#
# ```shell
# > docker ps | rb drop 1 | rb -l split[1]
# ubuntu
# postgres
# ```
#
# Display how much time ago containers have exited
#
# ```shell
# > docker ps -a | rb grep /Exited/ | rb -l 'split.last.ljust(20) + " => " + split(/ {2,}/)[-2]'
# angry_hamilton      => Exited (0) 18 hours ago
# dreamy_lamport      => Exited (0) 3 days ago
# prickly_hypatia     => Exited (0) 2 weeks ago
# ```
#
# Count files by their extension
#
# ```shell
# > find . -type f | rb 'group_by(&File.method(:extname)).map { |ext, o| "#{ext.chomp}: #{o.size}" }'
# : 3
# .rb: 19
# .md: 1
# ```
#
# ---
# source: https://github.com/thisredone/rb

File.join(Dir.home, ".rbrc").tap { |f| load f if File.exist?(f) }
def execute(_, code)
  puts _.instance_eval(&code)
rescue Errno::EPIPE
  exit
end

# single_line = ARGV[0] == "-l"
single_line = ARGV.delete("-l")
# code = eval("Proc.new { #{ARGV.join(' ')} }")
code = ARGV.drop(single_line ? 1 : 0).join(" ")
code = eval("Proc.new { #{code} }")
# single_line ? (execute($_, code) while $stdin.gets) : execute($stdin.readlines, code)
if single_line
  $stdin.each do |l|
    execute(l.chomp, code)
  end
else
  execute($stdin.each_line, code)
end
