+ 2

Ruby Array Hash what is that output code? Help with understabding this task

class Array def fun myhash = {:one => method(:reverse!),:two => method(:pop) } 3.times do myhash.each do |key,value| my hash[key].call end end end end array =[1, 2, 3, 4, 5] array.fun print array[0] -array[-1] # help with understabding

27th Jun 2020, 6:57 AM
Андрей Рыбаков
Андрей Рыбаков - avatar
1 Resposta
+ 2
3 раза вызывается каждый из ключей: 1) reverse! array=[5,4,3,2,1] pop array=[5,4,3,2] 2) reverse! array=[2,3,4,5] pop array=[2,3,4] 3) reverse! array=[4,3,2] pop array=[4,3] array[0]-array[1]=1
3rd Jul 2020, 4:35 PM
Julia Shabanova
Julia Shabanova - avatar