[英]Ruby - block scope
I have a method which I want to act differently depending on the scenario, so I want to be able to pass a block to the method, and execute if the block is given. 我有一个方法,根据情况要采取不同的操作,因此我希望能够将一个块传递给该方法,并在给出块的情况下执行。
However, I am confused by the scope of the variable in the block I am passing in. 但是,我对传入的块中变量的范围感到困惑。
For example: 例如:
def original_method (a, b, opt = {id: nil, id_map: {}})
element_id = (opt[:id_map])
yield if block_given?
end
And the new method which passes the block: 以及通过该块的新方法:
def new_method(a, b, opt)
original_method (a, b, opt) do
if(element_id.include? "some text")
puts "it has some text"
end
end
end
But I get the error: 但是我得到了错误:
undefined local variable or method `element_id'
at the yield line. 在屈服线上。
Can this be done? 能做到吗?
You need to pass the local variable element_id
, as yield
s argument. 您需要传递局部变量
element_id
作为yield
的参数。
def original_method (a, b, opt = {id: nil, id_map: {}})
element_id = opt[:id_map]
yield(element_id) if block_given? # pass it as argument
end
Then accept it like : 然后像这样接受它:
def new_method(a, b, opt)
original_method (a, b, opt) do | element_id | # receive as block pramater
if(element_id.include? "some text")
puts "it has some text"
end
end
end
element_id
local variable has been created inside the method original_method
, that is why it is accessible inside this method only. element_id
局部变量已经在original_method
方法内部创建,这就是为什么只能在此方法内部访问它的原因。
Inside the method new_method
, when you are calling the method original_method
with a block attached to it, due to closure capability it has access to all variables inside the method new_method
from begining to the point where the block is created. 在方法
new_method
,当您调用带有附加块的方法original_method
时,由于具有关闭功能,它可以访问方法new_method
从开始到创建块的所有变量。
Answer to your indirect question: 回答您的间接问题:
Blocks are scoped lexically , meaning that they have access to variables from the scope they're defined in (as opposed to "used in"). 块按词法作用域,这意味着它们可以从定义它们的作用域中访问变量(而不是“使用于”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.