No worries.
I’m not sure what you mean by method route. But you could write a method that does the exact same thing and just add it to the method chain there.
I’m probably going to muddy the waters here but collect! and map! are methods that can be called on an array. The square brackets create an array. Methods need objects. You created an Array object with the square brackets which is what allowed you to call the collect! and map! methods.
I’m pretty sure that’s as clear as mud for you so, here’s a code example that will shed some light on why to use a proc instead od a method. Let’s take the example you already posted.
cube = Proc.new { |x| x ** 3 }
[1, 2, 3].collect!(&cube)
==> [1, 8, 27]
[4, 5, 6].map!(&cube)
==> [64, 125, 216]
We could rewrite it this way:
[1,2,3].collect! do |x|
x ** 3
end
=> [1, 8, 27]
It works the exact same way as your code however, anytime I wanted to cube a number I would have to write that block each and every time. That could become tedious quickly. So we write the proc to keep ourselves from having to write the block over and over when we need this functionality.
Essentially blocks are a one use thing while a Proc is a block that is reusable; a method is just a method. I know, I’ve probably muddied the waters but this is the best way I can think to explain this.
As far as using a method to accomplish the same thing…you could in a variety if different ways but I think you can ignore this for the time being.
I’m hoping this shed some light on what you’re learning. Let us know if you need more help!