From: "Jesús Gabriel y Galán" Date: 2009-08-14T06:29:48+09:00 Subject: Re: New to ruby - how can I take this further? On Thu, Aug 13, 2009 at 10:56 PM, Stuart Cullum wrote: > Hi all > > I'm new to programming, and new to Ruby. > > I've written a little program listed below, a very simple math test, to > get into the swing of things. > > My question; could someone point out what I could do to make this more > elegant, and any mistakes etc.  - basically a critique would be most > welcome. This is how I'd do it straight away from your code: puts "Please enter your name:" first_name = gets.chomp number1 = rand(100) number2 = rand(100) # I'd use symbols for the operators # also note the snake_case convention operator_array = [:+, :-] #removed some unnecessary variables operator = operator_array[rand(2)] # Cleaner (IMHO) with string interpolation, # no need for .to_s question = "#{number1} #{operator} #{number2}" # We know the operators are methods of the numbers # so I'd rather send the method than eval a string answer = number1.send(operator, number2) puts "Welcome to MathTest!" # String interpolation rather than concatenation puts "#{first_name}, here is your first question:" puts question # get the answer as an integer user_answer = gets.chomp.to_i #no need to eval here, just compare the ints if user_answer == answer puts "Correct!" else puts "Incorrect. The answer is " + "#{answer}" end The next step could be to put the questions in a loop, so you don't need to type your name every time, and to count correct/incorrect answers and show a summary. Further, you could extend this to save to a file a count of correct/incorrect answers by name, and load this data at start and update it with the new session of answers. Good luck ! Jesus.