From: John W Higgins Date: 2009-09-30T02:51:32+09:00 Subject: Re: Sequel accessor methods --00c09f905c776f66530474bb1080 Content-Type: text/plain; charset=ISO-8859-1 Morning Jake, On Tue, Sep 29, 2009 at 9:42 AM, Jake Brightmatter wrote: > Let's assume for a second that I understood 50% of what you said. Based > on that half understood concept, the following code would look at house > locations in the hash and if the location was NIL, it would break out of > the loop? > > def find_unused_house_id() > i = 0 > b = true > while(b) > @open_lot = DataBase[:house].filter( :id => i ).first > b = false unless DataBase[:house][:id => i] > i += 1 > end > end > The following might actually be a little easier to understand - typos are not guaranteed to exist or not exist :) def find_unused_house_id(start_point = 0) while (DataBase[:house][:id => start_point]) start_point += 1 end start_point end This simply takes a starting point and will continue to loop through until you get a nil back from the database and then returns the id that got the nil back. I added the ability to pass in a starting point if you wanted to search from something other then 0 but if you just called the function without a value then it would start at 0 by default. It's slightly different then you posted but I think if you are having issues with syntax you should keep things nice and simple and this does that by merely returning the id and you can do as you please with the value once you've got it. The while line could read "while (DataBase[:house][:id => start_point] != nil)" but the != is not necessary in this case. Also, the reason for the start_point line at the bottom is that ruby will "return" the last value from the function and therefore just dropping the variable you want in as the last line works. You could use "return start_point" if you wanted but the standard is just to loose the return. Hope this helps some. John --00c09f905c776f66530474bb1080--