Skip to content

Instantly share code, notes, and snippets.

@yclim95
Forked from nextacademy-private/stylish_code.rb
Last active January 12, 2016 16:05
Show Gist options
  • Save yclim95/5352f5c9ea2f9b444c4f to your computer and use it in GitHub Desktop.
Save yclim95/5352f5c9ea2f9b444c4f to your computer and use it in GitHub Desktop.
Stylish Code
class Guessing_game
VALID_Numbers = (1..100).to_a # Store valid answers in an array
def initialize answer; @answer = answer
@solved = false
# Validate input
raise "Answer must be between 1 and 100" unless VALID_Numbers.include? @answer
end
def guess ( number )
if number == @answer # Check if the two are equal
@solved = true
:correct
elsif (number > @answer) # Check if the guess is higher
@solved = false
return :high
elsif(number<@answer) # Check if the guess is lower
@solved = false
:low
end
end
def solved?
@solved
end
end
game = Guessing_game.new(10)
# This following should print out a whole bunch of "true"
puts game.guess(5) == :low
puts game.guess(15) == :high
puts game.solved? == false
puts game.guess(10) == :correct
puts game.solved? == true
puts game.guess(2) == :low
puts game.solved? == false
begin
Guessing_game.new(200)
rescue RuntimeError => e
puts e.to_s == "Answer must be between 1 and 100"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment