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

Add a rescue modifier.

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

Kind regards

robert

···

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

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-forum.com/\.

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

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

Kind regards

robert

···

2007/7/19, Stefan Rusterholz <apeiros@gmx.net>:
        from :0

=> [nil, "a", "aaa", "aaaaaaa"]

(Or even without the parenthesis.)

Fred

···

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}

--
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)

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-forum.com/\.

I stand corrected. Sorry for the noise.

Kind regards

robert

···

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

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 !

···

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