Created
September 3, 2012 18:09
-
-
Save migane/3611582 to your computer and use it in GitHub Desktop.
input number with rack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# input_number.rb | |
def input_response(n) | |
answer = "" | |
if n > 0 | |
answer = "positive" | |
elsif n < 0 | |
answer = "negative" | |
else n == 0 | |
answer = "zero" | |
end | |
return answer | |
end | |
error = false | |
n = ARGV[0].to_i | |
def check_input | |
if ARGV.length < 1 || ARGV.length > 1 | |
error = true | |
elsif | |
begin | |
n = Integer(ARGV[0]) | |
rescue ArgumentError | |
error = true | |
end | |
end | |
return error | |
end | |
my_rack_proc = lambda { |env| [200, {"Content-Type" => "text/plain"}, | |
[check_input == true ? "Please, enter an integer as argument" :"The number you entered on the command line is " + input_response(n)]]} | |
puts my_rack_proc.call({}) |
def check_input
if ARGV.length < 1 || ARGV.length > 1
error = true
elsif #=> should be else not elsif
begin
n = Integer(ARGV[0])
rescue ArgumentError
error = true
end
end
return error
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice.