Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 1.43 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. desc "Am I sober?"
  2. task :before_deploy, :roles => :app do
  3.   check_start_hour = 21
  4.   check_end_hour = 4
  5.   tries = 3
  6.   time_limit = 10
  7.  
  8.   # check to see if we are in the "danger zone"
  9.   if (Time.now.hour > check_start_hour || Time.now.hour < check_end_hour)
  10.     correct = false
  11.     attempts = 0
  12.     while attempts < tries do
  13.       attempts += 1
  14.      
  15.       # define challenge problem
  16.       square_root = rand(10)+5
  17.       fact_root = rand(5)+1
  18.       correct_answer = factorial(fact_root) + square_root
  19.  
  20.       # prompt for guess (one foot in front of the other)
  21.       start_time = Time.now
  22.       set(:guess) do
  23.         Capistrano::CLI.ui.ask "What is the factorial of #{fact_root} plus the square root of #{square_root**2}? "
  24.       end
  25.       end_time = Time.now if guess
  26.  
  27.       # time limit check
  28.       if (end_time - start_time) > time_limit
  29.         puts "Too slow; try again"
  30.         next
  31.       end
  32.  
  33.       # Can you do math?  "Yes of course, Occifer!"
  34.       if (guess.to_i == correct_answer rescue false)
  35.         puts "You, sir or madam, are sober.  Deploy away!"
  36.         correct = true
  37.         break
  38.       else
  39.         puts "Incorrect.  It was #{correct_answer}"
  40.       end
  41.     end
  42.    
  43.     # Maybe this is a bad idea...
  44.     unless correct
  45.       puts "Have a drink of water first and try again."
  46.       return false
  47.     end
  48.   end
  49. end
  50.  
  51. def factorial(n)
  52.   if n == 0
  53.     1
  54.   else
  55.     n * factorial(n-1)
  56.   end
  57. end