Making class GuessingGame in Ruby, stuck on instance method guess -
okay, have assignment make class called guessinggame, , create..a guessing game! lol. seriously, have make game takes input integer that's between 1 , 10. have skeleton of class's syntax done, when instance method guess, i'm getting stuck. if user's guess higher random number, should return ":high". if it's lower number, returns ":low". if it's right number..you gist. think has conditionals. here of code far:
class guessinggame def initialize(answer) @answer = answer end answer = rand(10) + 1 def guess(guess) if guess.to_i > answer return ":high" elsif guess.to_i < answer return ":low" else return ":correct" end end def solved? end end
let me state here ruby noob, , i'm looking constructive advice here. here rspec errors:
guessinggame#guess returns :high when guess high guessinggame#guess returns :low when guess low guessinggame#guess returns :correct when guess correct guessinggame#guess changes solved? when correct guess made guessinggame#guess doesn't change solved? when incorrect guess made guessinggame#guess reflects last guess
after class definition, have set variable new instance of class, can call method.
class guessinggame attr_reader :answer def initialize @answer = rand(10) + 1 end def guess(guess) if guess.to_i > answer return ":high" elsif guess.to_i < answer return ":low" else return ":correct" end end def solved? end end = guessinggame.new puts a.guess(2)
since returning string, won't show unless call puts/print instead of returning, putting string instead. also, need attr_reader :answer otherwise, can't access answer outside of class , won't work. go read att accessors. lastly, can @answer = rand(10) + 1 directly.
Comments
Post a Comment