ruby methods that either yield or return Enumerator

后端 未结 4 648
死守一世寂寞
死守一世寂寞 2021-01-31 03:47

in recent versions of Ruby, many methods in Enumerable return an Enumerator when they are called without a block:

[1,2,3,4].map 
#=>         


        
4条回答
  •  有刺的猬
    2021-01-31 04:33

    easiest way for me

    class Array
      def iter
          @lam = lambda {|e| puts e*3}
          each &@lam
      end
    end
    
    array = [1,2,3,4,5,6,7]
    array.iter
    

    => 3 6 9 12 15 18 21

提交回复
热议问题