Skip to content

Instantly share code, notes, and snippets.

@vanmichael
Created November 17, 2013 04:31
Show Gist options
  • Save vanmichael/7509243 to your computer and use it in GitHub Desktop.
Save vanmichael/7509243 to your computer and use it in GitHub Desktop.
#Only validation that is not filtered is any guess that begins with 0 etc. 0123
class Guess
attr_accessor :guess, :number
def initialize
@number = 50
end
def valid_input?(input)
if (input !~ /\A\$?\d+(\.\d{1,2})?\z/) == false
return false
else
puts "Invalid"
return true
end
end
def get_guess
print "Guess a number between 0 and 1000:"
@guess = gets.chomp.to_s
get_guess if valid_input?(@guess) == true
end
def check_guess
if @guess.to_f < @number.to_f
puts "Too Low"
end
if @guess.to_f == @number.to_f
return "Correct"
end
if @guess.to_f > @number.to_f
puts "Too High"
end
end
end
#Only validation that is not filtered is any guess that begins with 0 etc. 0123
require './guess.rb'
require 'pry'
guesser = Guess.new
guesser.get_guess
while guesser.check_guess != "Correct" do
guesser.get_guess
puts "Congratulations, you guessed the number!"
end
#Only validation that is not filtered is any guess that begins with 0 etc. 0123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment