27. def play
puts "Welcome to the guessing game."
3.times do
guess = get_guess
if won? guess
@won = true
puts "You win!"
break
else
puts "Wrong!"
end
end
if !@won
puts "You lost.The actual number is #{@actual_number}."
end
end
28. def play
puts "Welcome to the guessing game."
3.times do
guess = get_guess
if won? guess
@won = true
puts "You win!"
break
else
puts "Wrong!"
end
end
if !@won
puts "You lost.The actual number is #{@actual_number}."
end
end
45. Code Constraints*
*Borrowed from Sandi Metz
Methods can be no longer than 5 lines.
Classes can be no longer than 100 lines.
Methods cannot take more than 4 arguments.
46. def play
puts "Welcome to the guessing game."
3.times do
guess = get_guess
if won? guess
@won = true
puts "You win!"
break
else
puts "Wrong!"
end
end
if !@won
puts "You lost.The actual number is #{@actual_number}."
end
end
54. def play_round
if correct_guess? get_guess
correct_guess
else
incorrect_guess
end
end
def incorrect_guess
puts "Wrong!"
false
end
def correct_guess
puts "Correct"
false
end
55. class Guess
def initialize actual_number
@actual_number = actual_number
@guess = get_guess
end
def status
if correct?
"Correct"
else
"Incorrect"
end
end
def correct?
guess == actual_number
end
private
attr_reader :guess, :actual_number
def get_guess
print "What is your guess: "
gets.to_i
end
end
56. class Guess
def initialize actual_number
@actual_number = actual_number
@guess = get_guess
end
def status
if correct?
"Correct"
else
"Incorrect"
end
end
def correct?
guess == actual_number
end
private
attr_reader :guess, :actual_number
def get_guess
print "What is your guess: "
gets.to_i
end
end
def play_round
guess = Guess.new(actual_number)
puts guess.display_status
guess.correct?
end