Array sort_by with nil elements

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

2007/7/19, Josselin [email protected]:

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

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

Robert K. 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

Le 19 juillet à 14:32, Robert K. 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

2007/7/19, Stefan R. [email protected]:

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

On 2007-07-19 14:56:40 +0200, Stefan R. [email protected] said:

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 !

2007/7/19, Stefan R. [email protected]:

Robert K. 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