A
Adam Akhtar
If i have a class
class Example
attr_accessor :value
def initialize
@value = 0
end
end
and I have an array of examples and want to sort them based on value how
do i define a <=> for my class. I know its possible as it was a side
comment on an earlier post. Seeing as this is a totally different
subject to that post i decided to ask in a new post - it will be easier
for others who may have the same questoin to search for.
i tried this
class Example
attr_accessor :value
def initialize
@value = 0
end
def <=>(other_example)
@value <=> other_example
end
end
but i keep getting an error of undefined method for nill class when i
try to call it like this with an array of example objects
ojects.sort do |one, another|
one.<=>(another)
end
i also tried
ojects.sort do |one, another|
one <=>(another)
end
(i.e no period between one and <=>)
where am i going wrong???
class Example
attr_accessor :value
def initialize
@value = 0
end
end
and I have an array of examples and want to sort them based on value how
do i define a <=> for my class. I know its possible as it was a side
comment on an earlier post. Seeing as this is a totally different
subject to that post i decided to ask in a new post - it will be easier
for others who may have the same questoin to search for.
i tried this
class Example
attr_accessor :value
def initialize
@value = 0
end
def <=>(other_example)
@value <=> other_example
end
end
but i keep getting an error of undefined method for nill class when i
try to call it like this with an array of example objects
ojects.sort do |one, another|
one.<=>(another)
end
i also tried
ojects.sort do |one, another|
one <=>(another)
end
(i.e no period between one and <=>)
where am i going wrong???