From: Robert Klemme Date: 2008-02-18T01:29:55+09:00 Subject: Re: where to check for nils or empty? On 16.02.2008 19:32, John Honovich wrote: > I am processing arrays of URLs. I call a series of methods to delete or > modify those urls. I have a risk that those methods will return an empty > or null array. > > I am trying to figure out where and how to best check if the array is > null or empty. > > > Right now, I am doing it like this: > > def process array > array = firstReview array > return if array.nil? || array.empty? > array = secondReview array > return if array.nil? || array.empty? > # repeat again and again > end > > def firstReview array > array.reject! {|a| @MasterList.include? (a)} > end > > > My approach works currently but feels ugly and repetitive. > > Any advice for improving this? There's a simple fix: do not assign to array. You can then do first_review array and second_review array and third_review Or you do reviews = [ lambda {|ar| ar.reject! {|a| ...}}, lambda {|ar| ar.reject! {|a| ...}}, lambda {|ar| ar.reject! {|a| ...}}, lambda {|ar| ar.reject! {|a| ...}}, ] def process array reviews.all? {|rev| rev[array]} end Btw, if the return value of your review is actually to mean something I'd rather define clear semantics aka "nil means terminate processing" and make sure that your review methods return the proper value. That's easier than checking multiple conditions all the time (empty? or nil?). Kind regards robert