0
我有這樣的λ:我如何正確範圍Ruby 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'
但是當我運行my_funky_lambda.rb這個拉姆達方法,我得到一個NameError。我不確定這個lambda的範圍有什麼問題,但我似乎無法從is_there_an_echo_in_here
訪問它。
echo_word
適當的作用域和使用,如果我把它作爲常量ECHO_WORD
並像這樣使用它,但必須有一個更直接的解決方案。
在這種情況下,訪問is_there_an_echo_in_here
內部的echo_word
lamba的最佳方式是什麼?將它包裝在一個模塊中,訪問全局範圍,還有其他的東西?
創建一個最小的測試情況下,在一個代碼塊。那麼你應該看到這個問題。它與'echo_word'的範圍(或缺少)有關。沒有關於lambda的信息。也可能是'x = 2; .. def y;做放x結束'顯示這個問題。 – 2013-03-20 21:29:55
哈哈公平點。看起來我已經花費了太多時間在節點上,並將其與:var a = 1; var b = function(){console.log(a); }; B()' – Hugo 2013-03-20 21:36:33