Skip to content

Instantly share code, notes, and snippets.

@daveyarwood
Last active March 23, 2017 13:56
Show Gist options
  • Save daveyarwood/3228f040f136d15e5445b39d24a8984e to your computer and use it in GitHub Desktop.
Save daveyarwood/3228f040f136d15e5445b39d24a8984e to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
template = <<~HEREDOC
{{plural noun}} are {{color}},
{{plural noun}} are {{color}},
{{food}} is {{adjective}},
and so are you.
HEREDOC
def prompt(type)
print type << ': '
gets.chomp
end
def replace_fields(template)
template.scan(/{{([^}]+)}}/).flatten.inject(template) do |result, type|
result.sub "{{#{type}}}", prompt(type)
end
end
madlib = replace_fields(template)
puts
puts madlib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment