
Untitled
By: a guest on
May 1st, 2012 | syntax:
None | size: 1.43 KB | hits: 14 | expires: Never
desc "Am I sober?"
task :before_deploy, :roles => :app do
check_start_hour = 21
check_end_hour = 4
tries = 3
time_limit = 10
# check to see if we are in the "danger zone"
if (Time.now.hour > check_start_hour || Time.now.hour < check_end_hour)
correct = false
attempts = 0
while attempts < tries do
attempts += 1
# define challenge problem
square_root = rand(10)+5
fact_root = rand(5)+1
correct_answer = factorial(fact_root) + square_root
# prompt for guess (one foot in front of the other)
start_time = Time.now
set(:guess) do
Capistrano::CLI.ui.ask "What is the factorial of #{fact_root} plus the square root of #{square_root**2}? "
end
end_time = Time.now if guess
# time limit check
if (end_time - start_time) > time_limit
puts "Too slow; try again"
next
end
# Can you do math? "Yes of course, Occifer!"
if (guess.to_i == correct_answer rescue false)
puts "You, sir or madam, are sober. Deploy away!"
correct = true
break
else
puts "Incorrect. It was #{correct_answer}"
end
end
# Maybe this is a bad idea...
unless correct
puts "Have a drink of water first and try again."
return false
end
end
end
def factorial(n)
if n == 0
1
else
n * factorial(n-1)
end
end