Hi!
So I’m currently working on the Bubble_sort! method which uses a proc but I’m having some difficulty understanding how it works. The test for this section is as follows:
it "will use a block if given" do
sorted = array.bubble_sort! do |num1, num2|
# order numbers based on descending sort of their squares
num2**2 <=> num1**2
end
expect(sorted).to eq([5, 4, 3, 2, 1])
end
So I thought that I’d write bubble_sort!(&prc), which uses prc.call to use the proc to check num2^2 <=> num1^2, where num1 and num2 are adjacent elements in the array.
However this didn’t work, upon checking the solution I see all I was missing was
`prc ||= Proc.new {|x,y| x<=>y}`
But I don’t understand what this is doing/why it’s necessary. If the block is being defined inside the method then we’re not really giving bubble_sort! a proc are we? And what is the point of giving bubble_sort! a block in the test if we’re just going to redefine prc in the first line of our method?
Thanks!
-Willis