[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

array sort_by with nil elements

Josselin

7/19/2007 6:57:00 AM

I am using the sort_by command

@buddies = @user.buddies.compact.sort_by {|item| item.send @criteria}


problem arise when one criteria like the user.name is nil the sort
crash.... anyway to avoid it

thanks a lot for your lights

joss

7 Answers

Robert Klemme

7/19/2007 8:28:00 AM

0

2007/7/19, Josselin <josselin@wanadoo.fr>:
> I am using the sort_by command
>
> @buddies = @user.buddies.compact.sort_by {|item| item.send @criteria}
>
>
> problem arise when one criteria like the user.name is nil the sort
> crash.... anyway to avoid it

Add a rescue modifier.

@buddies = @user.buddies.compact.sort_by {|item| item.send @criteria rescue nil}

Kind regards

robert

Stefan Rusterholz

7/19/2007 10:21:00 AM

0

Josselin wrote:
> I am using the sort_by command
>
> @buddies = @user.buddies.compact.sort_by {|item| item.send @criteria}
>
>
> problem arise when one criteria like the user.name is nil the sort
> crash.... anyway to avoid it
>
> thanks a lot for your lights
>
> joss

Simply add a default value (best one that gets sorted in the beginning
or the end)
@buddies = @user.buddies.compact.sort_by {|item| item.send(@criteria) ||
default }


Regards
Stefan

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

Robert Klemme

7/19/2007 12:32:00 PM

0

2007/7/19, Stefan Rusterholz <apeiros@gmx.net>:
> Josselin wrote:
> > I am using the sort_by command
> >
> > @buddies = @user.buddies.compact.sort_by {|item| item.send @criteria}
> >
> >
> > problem arise when one criteria like the user.name is nil the sort
> > crash.... anyway to avoid it
> >
> > thanks a lot for your lights
> >
> > joss
>
> Simply add a default value (best one that gets sorted in the beginning
> or the end)
> @buddies = @user.buddies.compact.sort_by {|item| item.send(@criteria) ||
> default }

This does not work.

irb(main):003:0> [nil].sort_by {|it| it.send(:length) || 0}
NoMethodError: undefined method `length' for nil:NilClass
from (irb):3:in `send'
from (irb):3
from (irb):3:in `sort_by'
from (irb):3:in `each'
from (irb):3:in `sort_by'
from (irb):3
from :0

You actually need to deal with the exception or test beforehand that
the item responds to the method.

Kind regards

robert

F. Senault

7/19/2007 12:55:00 PM

0

Le 19 juillet à 14:32, Robert Klemme a écrit :

> This does not work.
>
> irb(main):003:0> [nil].sort_by {|it| it.send(:length) || 0}

>> [nil, "aaaaaaa", "aaa", "a" ].sort_by {|it| (it && it.send(:length)) || 0}
=> [nil, "a", "aaa", "aaaaaaa"]

(Or even without the parenthesis.)

Fred
--
An ASCII character walks into a bar and orders a double. 'Having a bad
day?' asks the barman. 'Yeah, I have a parity error,' replies the ASCII
character. The barman says, 'Yeah, I thought you looked a bit off.'
(Kirrily 'Skud' Robert)

Stefan Rusterholz

7/19/2007 12:57:00 PM

0

Robert Klemme wrote:
> This does not work.
>
> irb(main):003:0> [nil].sort_by {|it| it.send(:length) || 0}
>
> Kind regards
>
> robert

According to him, his problem is not the *element* being nil, but the
*method called upon* returning nil. So yes, this does work for the
problem he describes.
>> problem arise when one criteria like the user.name is nil

Regards
Stefan

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

Robert Klemme

7/19/2007 1:07:00 PM

0

2007/7/19, Stefan Rusterholz <apeiros@gmx.net>:
> Robert Klemme wrote:
> > This does not work.
> >
> > irb(main):003:0> [nil].sort_by {|it| it.send(:length) || 0}
>
> According to him, his problem is not the *element* being nil, but the
> *method called upon* returning nil. So yes, this does work for the
> problem he describes.
> >> problem arise when one criteria like the user.name is nil

I stand corrected. Sorry for the noise.

Kind regards

robert

Josselin

7/19/2007 9:47:00 PM

0

On 2007-07-19 14:56:40 +0200, Stefan Rusterholz <apeiros@gmx.net> said:

> Robert Klemme wrote:
>> This does not work.
>>
>> irb(main):003:0> [nil].sort_by {|it| it.send(:length) || 0}
>>
>> Kind regards
>>
>> robert
>
> According to him, his problem is not the *element* being nil, but the
> *method called upon* returning nil. So yes, this does work for the
> problem he describes.
>>> problem arise when one criteria like the user.name is nil
>
> Regards
> Stefan

yes you're right , I did not mention it (even thought...) the element
'user' always exist... but user can have nil display_name....

thanks to all , I keep that in my book !