From: Paul McMahon Date: 2008-04-03T14:30:46+09:00 Subject: Re: ruby constructor return value? Brendan Stennett wrote: > > In ruby.. > class Zip > def initialize(zipcode) > o = Zipcode.find_by_zipcode(zipcode) > if o.length == 0 > return false > else > @zipcode = zipcode > end > end > end > > //then... > if o = Zip.new('00000') > //this block execute whether or not that zip code exists > else > //never happens > end > > ..now i know there are easier ways to do this, but its just an example > to illustrate my point. Maybe I'm missing something, but why not just push the creation into Zipcode. I can't see why you would want to do this check in the constructor itself... class Zipcode def self.find_by_zipcode(zipcode) zipcode = ... # query db if zipcode.length == 0 nil else Zip.new(zipcode) end end end class Zip def initialize(zipcode) @zipcode = zipcode end end //then... if o = Zip.find_by_zipcode('00000') // zipcode exists else // doesn't exist end