我有这个 lambda:
echo_word = lambda do |words|
  puts words
  many_words = /\w\s(.+)/
    2.times do
      sleep 1
      match = many_words.match(words)
      puts match[1] if match
    end
  sleep 1
end
我想将它each作为一个块传递给,将来每个块都会传递更多。
def is_there_an_echo_in_here *args
  args.each &echo_word # throws a name error
end
is_there_an_echo_in_here 'hello out there', 'fun times'
但是当我用这个 lambda 方法运行 my_funky_lambda.rb 时,我得到了一个 NameError。我不确定这个 lambda 的范围是怎么回事,但我似乎无法从is_there_an_echo_in_here.
echo_word如果我将其设为常量ECHO_WORD并像那样使用它,那么它的范围和使用是正确的,但必须有一个更直接的解决方案。
在这种情况下,从内部访问echo_wordlamba的最佳方式是什么is_there_an_echo_in_here,例如将其包装在模块中、访问全局范围等?