[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

where to check for nils or empty?

John Honovich

2/16/2008 6:33:00 PM

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?

Thanks,

John
--
Posted via http://www.ruby-....

7 Answers

Christopher Dicely

2/16/2008 10:25:00 PM

0

Maybe something like this?

class Whatever
def initialize
...
@reviews = [:firstReview, :secondReview, ...]
end

def process array
@reviews.inject(array) do |arr, review|
break if arr.nil? || arr.empty?
send(review, arr)
end
end

def firstReview array
array.reject! {|a| @MasterList.include? (a)}
end
end

On Feb 16, 2008 10:32 AM, John Honovich <jhonovich@gmail.com> 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?
>
> Thanks,
>
> John
> --
> Posted via http://www.ruby-....
>
>

7stud --

2/17/2008 1:30:00 AM

0

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.
>
> My approach works currently but feels ugly and repetitive.
>
> Any advice for improving this?
>

Some counter advice: inject() is the definition of slow and ugly. You
should reject any code that uses inject() out of hand.
--
Posted via http://www.ruby-....

Robert Klemme

2/17/2008 4:30:00 PM

0

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

Dominik Honnef

2/17/2008 6:35:00 PM

0

On [Mon, 18.02.2008 01:29], Robert Klemme wrote:
> 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


Your first approach won't work, as an empty array evaluates to true,
but the processing should stop on empty arrays
--
Dominik Honnef

Robert Klemme

2/18/2008 8:14:00 AM

0

2008/2/17, Dominik Honnef <dominikho@gmx.net>:
> On [Mon, 18.02.2008 01:29], Robert Klemme wrote:
> > 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?).
>
> Your first approach won't work, as an empty array evaluates to true,
> but the processing should stop on empty arrays

Right. Thanks for pointing it out! That's why I suggested to define
the return value of the method to mean something and not just
accidentally use what bang methods return. See my remark in the last
paragraph.

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

Todd Benson

2/18/2008 5:06:00 PM

0

On Feb 16, 2008 4:24 PM, Christopher Dicely <cmdicely@gmail.com> wrote:
> Maybe something like this?
>
> class Whatever
> def initialize
> ...
> @reviews = [:firstReview, :secondReview, ...]
> end
>
> def process array
> @reviews.inject(array) do |arr, review|
> break if arr.nil? || arr.empty?
> send(review, arr)
> end
> end

You could also build the original array beforehand...

(array - [[]]).compact

Todd

Todd Benson

2/18/2008 5:14:00 PM

0

On Feb 18, 2008 11:05 AM, Todd Benson <caduceass@gmail.com> wrote:
> On Feb 16, 2008 4:24 PM, Christopher Dicely <cmdicely@gmail.com> wrote:
> > Maybe something like this?
> >
> > class Whatever
> > def initialize
> > ...
> > @reviews = [:firstReview, :secondReview, ...]
> > end
> >
> > def process array
> > @reviews.inject(array) do |arr, review|
> > break if arr.nil? || arr.empty?
> > send(review, arr)
> > end
> > end
>
> You could also build the original array beforehand...
>
> (array - [[]]).compact

Also, depending on your data, you might have to do it backwards...

array.compact - [[]]

Todd