+ 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
1 Answer
+ 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