From: Rodrigo Bermejo Date: 2009-10-11T01:28:03+09:00 Subject: Re: Trouble with local variables Severin Newsom wrote: > Hello everyone! > I've been coding with Ruby for about three days, and I just ran into my > first big snag. I've been working on a quiz, and the first one had > questions like this: > > puts 'How many classes have a taunt kill?' > puts 'A - Three' > puts 'B - Four' > puts 'C - Five' > puts 'D - Six' > answer = gets.chomp.downcase > if (answer == 'd') > score = score + 1 > else > end > > But it's messy, since I have to repeat the code for every question. (I > know, I know, DRY). With this in mind, I began construction on quiz2, > and used this as my prototype: > > def check > answer = gets.chomp.downcase > if (answer == c_answer) > score = score + 1 > else > puts 'The correct answer was ' + c_answer + '.' > end > end > puts 'Which is the slowest class?' > puts 'A - Pyro' > puts 'B - Demoman' > puts 'C - Heavy' > puts 'D - Soldier' > c_answer = 'c' > check > > I understand the problem (mostly), but I'm looking for a workaround. > Most of my 'fixes' don't work because of the 'A, B, C, D' system; should > that system be changed? > > Sorry for such a long post, I've just really gotten into this. read about class variables (or global variables) @score Also it will be cleaner if you pass the answer to chech() tru its argument list def check(answer) ..play around. -- Posted via http://www.ruby-forum.com/.