array sort_by with nil elements

J

Josselin

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
 
R

Robert Klemme

2007/7/19 said:
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
 
S

Stefan Rusterholz

Josselin said:
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
 
R

Robert Klemme

2007/7/19 said:
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

F. Senault

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
 
S

Stefan Rusterholz

Robert said:
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.
Regards
Stefan
 
R

Robert Klemme

2007/7/19 said:
Robert said:
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.

I stand corrected. Sorry for the noise.

Kind regards

robert
 
J

Josselin

Robert said:
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.
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 !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top