1
假设我有一个数组:如何从枚举器方法内引用集合?
arr = [53, 55, 51, 60]
现在我把一些枚举法就可以了。剥离下来的例子:
arr.each_with_index { |e, i| puts "Element #{i} of #{arr.length} is #{e}" }
#=> Element 0 of 4 is 53
#=> Element 1 of 4 is 55
#=> Element 2 of 4 is 51
#=> Element 3 of 4 is 60
如果我更改到:
[1, 10, 100].each_with_index {|e, i| puts "Element #{i} of #{arr.length} is #{e}" }
#=> Element 0 of 4 is 1
#=> Element 1 of 4 is 10
#=> Element 2 of 4 is 100
哪项是错误的,因为arr
仍引用外部变量。
有没有一种方法可以从枚举器方法内引用回集合?
在lambda? ' - > x {x.each_with_index {| e,i |放置“#{x.length}的元素#{i}是#{e}”}}。([1,10,100])'我也想知道':)' –