I'm trying to call a method I defined earlier in the code and it gives me this error:
"undefined method `filter' for 0:Integer (NoMethodError)"
This is the code:
def descending_order(n)
@arrNum = n.to_s.chars.map {|i| i.to_i }
@final = [@arrNum[0]]
def self.filter(arr, ind)
if self > arr[ind]
@final.insert(ind, self)
else
self.filter(arr, ind 1)
end
end
@arrNum.each { |i| i.filter(@final, 0) }
return @final
end
I tried everything I could think of! Thanks
CodePudding user response:
Your approach is wrong!
If your intention is to return a reverse sorted array of a given number then the code snippet should be:
def descending_order(n)
n.digits.sort.reverse
end
The above snippet returns following output
puts descending_order(352614)
=> [6, 5, 4, 3, 2, 1]