@instance_variable 在 ruby 块内不可用?
使用以下代码:
def index
@q = ""
@q = params[:search][:q] if params[:search]
q = @q
@search = Sunspot.search(User) do
keywords q
end
@users = @search.results
end
如果使用 @q 而不是 q,则搜索始终返回空查询 ("") 的结果。这是为什么呢? @q 变量对 do...end 块不可用吗?
With the following code:
def index
@q = ""
@q = params[:search][:q] if params[:search]
q = @q
@search = Sunspot.search(User) do
keywords q
end
@users = @search.results
end
If @q is used instead of q, the search always returns results for an empty query (""). Why is this?
Is the @q variable unavailable to the do...end block?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于块的调用方式。如果使用
yield
关键字或Proc#call
方法调用它,那么您将能够在块中使用实例变量。如果使用Object#instance_eval
或Module#class_eval
调用它,则块的上下文将被更改,并且您将无法访问实例变量。在您的情况下,
Sunspot.search
似乎正在使用instance_eval
在不同的上下文中调用您的块,因为该块需要轻松访问该关键字
方法。It depends on how the block is being called. If it is called using the
yield
keyword or theProc#call
method, then you'll be able to use your instance variables in the block. If it's called usingObject#instance_eval
orModule#class_eval
then the context of the block will be changed and you won't be able to access your instance variables.In your case, it looks like
Sunspot.search
is calling your block in a different context usinginstance_eval
, because the block needs easy access to thatkeywords
method.正如 Jeremy 所说,Sunspot 在新的范围内执行其搜索 DSL。
为了在 Sunspot.search 块中使用实例变量,您需要向其传递一个参数。像这样的东西应该有效(未经测试):
请参阅此处以获得更好的解释:http: //groups.google.com/group/ruby-sunspot/msg/d0444189de3e2725
As Jeremy says, Sunspot executes its search DSL in a new scope.
In order to use an instance variable in the Sunspot.search block, you'll need to pass it an argument. Something like this should work (not tested):
See here for a better explanation: http://groups.google.com/group/ruby-sunspot/msg/d0444189de3e2725