Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ruby中是一个可枚举的';这和收益率有什么相似之处吗?_Ruby_Yield_Control Flow_Enumerable - Fatal编程技术网

在Ruby中是一个可枚举的';这和收益率有什么相似之处吗?

在Ruby中是一个可枚举的';这和收益率有什么相似之处吗?,ruby,yield,control-flow,enumerable,Ruby,Yield,Control Flow,Enumerable,我试图探索在函数中定期使用yield与使用可枚举的yielder之间的区别 通过定期使用yield我的意思是: def fun yield end fun do puts "hello" end 我所说的统计员的意思是: def fun2 Enumerator.new do |yielder| (0..2).each { |x| yielder << x } end end fun2.each do |x| puts x end def fun2

我试图探索在函数中定期使用
yield
与使用可枚举的
yielder
之间的区别

通过定期使用
yield
我的意思是:

def fun
  yield
end

fun do
  puts "hello"
end
我所说的统计员的意思是:

def fun2
  Enumerator.new do |yielder|
    (0..2).each { |x| yielder << x }
  end
end

fun2.each do |x|
  puts x
end
def fun2
Enumerator.new do | yielder|
(0..2).每次我都会讨论这个问题,这可能会有所帮助。