[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Using the spaceship operator to compare nil?

Dan Croak

8/9/2007 7:16:00 PM

Hey folks,

I have a test helper in a Rails app called assert_ordered_by

def assert_ordered_by(attribute, collection, direction)
collection = collection.to_s
return true if assigns["#{collection}"].length < 2
option_one = assigns["#{collection}"][0].send(attribute)
option_two = assigns["#{collection}"][1].send(attribute)
assert option_one.send(direction, option_two), "#{option_one} is not
#{direction} #{option_two}"
end

It works well in all the cases I've tried thus far except when dealing
with nil. I want to teach the method to treat nil the same way SQL
treats NULL because the collections will often contain ActiveRecord
objects.

I have the idea in my head that I need to use the spaceship operator
(<=>) to do this, but am unsure about how...

Any ideas?

Thanks,
Dan Croak
http://thoug...

5 Answers

Dan Croak

8/9/2007 9:26:00 PM

0

I forgot to mention that this is called, for example, with:

assert_ordered_by :last_name, :users, "<="

I'm also moving away from using the spaceship operator into
conditional logic land because I was having difficulty dealing
(mentally) with comparing nil to various types (dates, strings,
integers, etc.). Here's the latest non-working code... :/

def assert_ordered_by(attribute, collection, direction)
collection = assigns["#{collection.to_s}"]
return true if collection.length < 2
collection.sort do |a, b|
if a.nil? && !b.nil?
return false if direction.include?(">") # nil cannot be
greater than anything except another nil
elsif b.nil? && !a.nil?
return false if direction.include?("<") # nil cannot be
greater than anything except another nil
elsif !a.nil? && !b.nil?
assert a.send(attribute).send(direction, b.send(attribute))
elsif a.nil? && b.nil?
return true
end
end
end

Gregory Brown

8/9/2007 9:40:00 PM

0

On 8/9/07, Dan Croak <dan.croak@nationalgazette.org> wrote:
> I forgot to mention that this is called, for example, with:
>
> assert_ordered_by :last_name, :users, "<="
>
> I'm also moving away from using the spaceship operator into
> conditional logic land because I was having difficulty dealing
> (mentally) with comparing nil to various types (dates, strings,
> integers, etc.). Here's the latest non-working code... :/

Try this:

has_nil, doesnt = collection.partition { |r| r.send(attribute).nil? }

you can then sort only the ones that have a meaningful sort value and
then rejoin as needed based on the direction, e.g. for one directed
you want:

doesnt.sort + has_nil

and for another you want

has_nil + doesn't.sort

hope that helps. Excuse the incomplete code :)

Dan Croak

8/10/2007 9:50:00 PM

0

On Aug 9, 5:39 pm, "Gregory Brown" <gregory.t.br...@gmail.com> wrote:
> Try this:
>
> has_nil, doesnt = collection.partition { |r| r.send(attribute).nil? }
>
> you can then sort only the ones that have a meaningful sort value and
> then rejoin as needed based on the direction, e.g. for one directed
> you want:
>
> doesnt.sort + has_nil
>
> and for another you want
>
> has_nil + doesn't.sort
>
> hope that helps. Excuse the incomplete code :)

Thanks, Gregory. This is what I ended up with (works well) ~

def assert_ordered_by(attribute, collection, direction)
collection = assigns["#{collection.to_s}"]
return true if collection.length < 2
has_nil, has_no_nil = collection.partition { |r|
r.send(attribute).nil? }
has_no_nil = has_no_nil.sort_by(&:"#{attribute}")
expected = has_nil + has_no_nil
expected.reverse! if direction.include?(">")
assert_equal expected.map(&:"#{attribute}"),
collection.map(&:"#{attribute}")
end

Dug

2/14/2011 8:40:00 PM

0

"The Revd" <peelingthe@invalid.admin> wrote in message
news:2tajl6t9l167n9a3jdqlemso8fabpc3n2u@4ax.com...
> On Mon, 14 Feb 2011 09:51:10 -0800, "Dug" <andxornot@gggmail.com>
> wrote:
>
>>"tdny" <tdny@live.com> wrote in message
>>news:urGdnfUKJ6XC-sTQnZ2dnUVZ_gednZ2d@earthlink.com...
>>>
>>>
>>> http://www.youtube.com/watch?v=8...
>>
>>I'm sure the lieberal assholes of the world have no problem w/the
>>above. I just hope they get the same treatment.
>
> I have no problem w/it. I just hope you get hanged for sucking jew
> rectums one day.

Your obsession w/sucking rectum is really all your own.


The Peeler

2/14/2011 11:04:00 PM

0

On Mon, 14 Feb 2011 12:40:18 -0800, "Dug" <andxornot@gggmail.com>
wrote:

>"The Revd" <peelingthe@invalid.admin> wrote in message
>news:2tajl6t9l167n9a3jdqlemso8fabpc3n2u@4ax.com...
>> On Mon, 14 Feb 2011 09:51:10 -0800, "Dug" <andxornot@gggmail.com>
>> wrote:
>>
>>>"tdny" <tdny@live.com> wrote in message
>>>news:urGdnfUKJ6XC-sTQnZ2dnUVZ_gednZ2d@earthlink.com...
>>>>
>>>>
>>>> http://www.youtube.com/watch?v=8...
>>>
>>>I'm sure the lieberal assholes of the world have no problem w/the
>>>above. I just hope they get the same treatment.
>>
>> I have no problem w/it. I just hope you get hanged for sucking jew
>> rectums one day.
>
>Your obsession w/sucking rectum is really all your own.

The obsession is yours, Yitzhak. You do it 24/7/365. Enjoy already!
Mazel tov! L'chaim!