From: Fabian Streitel Date: 2009-08-15T01:25:38+09:00 Subject: Re: New to ruby - how can I take this further? --001517588560533d2b04711c807a Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit just some more suggestions: 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 = [:+, :-] I personally prefer %w: operator_array = %w{+ -} Especially when you get more entries, %w helps you save time and maintain readability. Basically, what it does is, it splits the string in between the {} at every whitespace and returns an Array containing the Strings inbetween the whitespace. #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) Also, you should steer clear of eval, since it is quite a security risk. Users could give you any Ruby code, even stuff that deletes files etc. I've found that most of the time you can get rid of it. 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}" > you missed one! :-) puts "Incorrect. The answer is #{answer}" > > end > Keep it up! Hope you have lots of fun with Ruby! Greetz! --001517588560533d2b04711c807a--