Skip to content

Instantly share code, notes, and snippets.

@vdinovi
Last active May 15, 2020 19:59
Show Gist options
  • Select an option

  • Save vdinovi/807d65b82f4df9e5f2ac8551610be90b to your computer and use it in GitHub Desktop.

Select an option

Save vdinovi/807d65b82f4df9e5f2ac8551610be90b to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'yaml'
VERBS = %w(pick, usage).freeze
NEGATE_CHAR = '~'
POOLS = YAML.load_file("/Users/vdinovi/bin/victims_pool.yml")
USAGE = <<~USAGE
Picks a random victim from the intersection pools for horrific task like Code Review
Usage: victim <verb> <pool1>:<pool2>:!<pool3>...
verbs:
#{VERBS.map { |verb| "- #{verb}" }.join("\n")}
pools:
#{POOLS.map { |pool, members| "- #{pool}: #{members.join(', ')}" }.join("\n")}
USAGE
unless ARGV.count == 2
puts(USAGE)
exit 1
end
unless (verb = ARGV[0])
puts("Invalid verb #{verb}\n#{USAGE}")
exit 1
end
pools = ARGV[1]&.split(':') || []
invalid = pools.reduce([]) do |list, pool|
key = pool[0] == NEGATE_CHAR ? pool[1..] : pool
POOLS.has_key?(key) ? list : list + [pool]
end
if invalid.any?
puts("Invalid pools #{invalid.join(', ')}\n#{USAGE}")
exit 1
end
def pick(pools)
pools
.partition { |pool| pool[0] != NEGATE_CHAR }
.map { |keys| keys.map { |key| POOLS[key[0] == NEGATE_CHAR ? key[1..] : key] }.reduce(&:&) || [] }
.reduce(&:-)
.sample
end
case verb
when 'usage'
puts USAGE
when 'pick'
puts "And the victim is... #{pick(pools)}"
end
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment