From: Robert Klemme Date: 2008-02-11T21:24:53+09:00 Subject: Re: New user - correct way to think and write code 2008/2/11, Ashley Wharton : > Hi - sorry to belabour the point, but I would appreciate any help or > guidance that anyone could give relative to the preferred way of writing > Ruby code. Thanks to some help earlier in the week from ruby-talk and from > Kastner I now have two additional ways of achieving the same result. I am > attempting to grasp the concept objects as well as learning ruby. Which > syntax (or style) should I focus on, or does it not matter. From my limited > understanding Kastner's example would seem to be a cleaner use of objects, > (or am I missing the point). Thanks for any help. ... > With further help from Kastner: > > MAX = 100000 > > def get_number(prompt = "") > print prompt > # ruby methods return the last value > gets.to_i > end > > def print_from_to(start, stop) > start.upto(stop) do |number| > puts number > end > end > > loop do > number = get_number "Please enter a number less than #{MAX}: " > next if number >= MAX > print_from_to(number, MAX) > break > end As far as I can see you logic is: read user input until it is valid and then print from input to MAX. I would remove the printing from the loop because that is much cleaner. Kind regards robert -- use.inject do |as, often| as.you_can - without end