I am getting confused with the Array methods below. Can anyone help me understand how differently they work from each other with the help of simple snippet?
array.sortandarray.sort { | a,b | block }array.to_aandarray.to_aryarray.sizeandarray.lengtharray.reverseandarray.reverse_each {|item| block }array.fill(start [, length] ) { |index| block }and
array.fill(range) { |index| block }
Please read the documentation for Array.
sort:
use the second one if you need some custom way to sort elements.
to_a vs. to_ary:
size and length are exactly the same.
reverse_each is pretty much the same as reverse.each.
If you want to fill only a part of the array, you can call Array.fill either with a
rangeorstart,length. Those are just different ways to achieve the same:both return
["a", "a", "b", "b", "b", "b", "b", "b", "a", "a"].