From: Josh Cheek Date: 2011-05-05T16:51:13+09:00 Subject: Re: Lets play a guessing game. (how to code this better?) --0016e6469c1af4f3f004a282a205 Content-Type: text/plain; charset=ISO-8859-1 On Thu, May 5, 2011 at 1:45 AM, Super Goat wrote: > I am a new Rubyist. I told my friend that I was learning Ruby. He asked > me how that was going and then gave me a little challenge. His > challenge, "Write a text game that guesses numbers 0-100". My reply, > "you mean it picks a number at random, and you guess the number, it > tells you higher or lower until you get it?". Up to this point I had not > coded anything in Ruby on my own (aside from the examples in the book) > and I saw this as a great first challenge. So here is what I got. It > took me some time. I ran into trouble because I forgot to take into > consideration the Class of the variables and couldn't figure out why the > loops weren't working. > My question to you all... how could I have done this better or do you > seeing anything that is wrong. Attached is the code. Thanks for the > feedback. > > Attachments: > http://www.ruby-forum.com/attachment/6168/100guess.rb > > Hi, Super Goat :) Congratulations on your first program! Hope you're enjoying Ruby. Here are some thoughts, that might be helpful: * On line1 your shebang is `#!/usr/bin/ruby`, but the best shebang is `#!/usr/bin/env ruby` because Ruby can be located in lots of different places, but env is always (I think) located in /usr/bin/env, and it is smart enough to figure out where your real Ruby is and then invoke it. * On line 4, you have `rand(100).to_i`, rand already returns an integer when you pass it a parameter, so you don't need to use to_i on it ( http://www.ruby-doc.org/core/classes/Kernel.html#M001388) * For that variable, I would have probably named it "target" rather than "random", I think this clearer. * On line 11, `if choice > random.to_i` again you are converting it to an integer, but it is already an integer. (not harmful, but redundant and thus confusing as it makes reader say "wait, I thought it was an integer already, what did I miss?") * Lines 13-14 look like this: if choice > random.to_i puts "\nIncorrect, please choose a lower number." else puts "\nIncorrect, please choose a higher number." end Your indenting is off, the if/else/end should be at the same level, and for multiline conditionals, the code would go between them, so it should look like this: if choice > target puts "\nIncorrect, please choose a lower number." else puts "\nIncorrect, please choose a higher number." end I would personally probably save space by putting the conditional into the string like this: puts "\nIncorrect, please choose a #{if choice > target then 'higher' else 'lower' end} number" I'm not sure whether many other Rubyists would do that or not, but I like it much better. * On line 16, you have gets.chomp.to_i The chomp here is unnecessary, because to_i will disregard anything after the digit. --- Anyway, nice job. Keep it up, after you get comfortable with the language, you can play with gems, which is where all the fun stuff is! --0016e6469c1af4f3f004a282a205--