Gradual automation in Ruby

Gradual automation in Ruby

It’s the simplest piece of Ruby code you’ll read today. Also known as: do-nothing scripting. Original inspiration.

Problem

You want to codify a manual process like setting up another instance of your e-commerce app. It may involve several steps with varying potential for automation (like seed the db, set up a subdomain, set up admin account).

  • Solution 1 šŸ“–: lay out all steps in a wiki page and teach people to conform.
  • Solution 2 šŸ§ : don’t even document the steps, keep it in your own head. Have people always come to you or discover it from scratch, develop tribal knowledge.
  • Solution 3 šŸ–²: make everything happen at the push of a button in your glossy dashboard app. Spend weeks implementing it and months maintaining it. Lie to yourself that this is good ROI.
  • Solution 4 āš™ļø: skip the UI part, write a script that automates it all. Wonder why people don’t use it.
  • Solution 5 šŸ“ + āš™ļø: make a do-nothing script that only tells you what to do next. Gradually automate it where it makes sense.

Advantages of a do-nothing script

  • It’s version controlled just as the rest of your stuff.
  • It’s easy to start with ā€” at the beginning nothing needs to be automated.
  • It can keep track of your progress
  • You can automate some steps, leave the rest to be done manually

An example

The original example is in Python. This is how I once did it in Ruby. I hereby announce another name for this technique: Puts-Driven Automation.

STEPS = [
  -> {
    puts "Create a user"
    puts
    puts
    puts "u = User.create!("
    puts "  name: '#{name}',"
    puts "  email: '#{email}',"
    puts "  company: '#{company}',"
    puts ")"
  },
  -> {
    puts "Setup an account on a 3rd party service"
    puts
  },
]

def ask_to_continue
  puts 'Continue? [Y/n]'
  input = STDIN.gets.chomp
  unless input == '' || /^[Yy]$/.match(input)
    puts 'User cancelled.'
    exit
  end
end

STEPS.each_with_index do |step, i|
  puts "-----------------------------------------------------------------------"
  puts "Step #{i}"
  puts "-----------------------------------------------------------------------"
  puts
  step.call
  puts
  ask_to_continue if i < (STEPS.size - 1)
end

I bet you can make the above snippet better!

Send me a gist showing how you do it and I’ll link your example here. DMs open.

Got comments? Reply under this tweet.

Source: Arkency