R
Robert Evans
Hi,
I am recently learning Ruby, and came across an interesting problem
that I do not understand. I was trying to count how many times the sort
method was called on an object by overridding <=> to count the number
of calls.
I decided to extend Fixnum as follows, but it didn't work. Can anyone
tell me why? Thanks.
Bob Evans
Code:
class Fixnum
@@count = 0
alias originalComparator <=>
def <=>(o)
@@count += 1
originalComparator(o)
end
def Fixnum.count
@@count
end
end
Running against it:
Expected => 6 > result > 0
Now, if I call 1<=>2 then count increments. Does this mean that Fixnum
sorting does not call those operators? Thanks for any help.
I am recently learning Ruby, and came across an interesting problem
that I do not understand. I was trying to count how many times the sort
method was called on an object by overridding <=> to count the number
of calls.
I decided to extend Fixnum as follows, but it didn't work. Can anyone
tell me why? Thanks.
Bob Evans
Code:
class Fixnum
@@count = 0
alias originalComparator <=>
def <=>(o)
@@count += 1
originalComparator(o)
end
def Fixnum.count
@@count
end
end
Running against it:
=> [5,4,3,2,1]array = [5, 4, 3, 2, 1]
=> [1,2,3,4,5]array.sort
=> 0Integer.count
Expected => 6 > result > 0
Now, if I call 1<=>2 then count increments. Does this mean that Fixnum
sorting does not call those operators? Thanks for any help.