Ruby on rails 你打什么电话?

Ruby on rails 你打什么电话?,ruby-on-rails,ruby,Ruby On Rails,Ruby,I此方法,其中最后使用.call: def allow?(controller, action, resource = nil) allowed = @allow_all || @allowed_actions[[controller.to_s, action.to_s]] allowed && (allowed == true || resource && allowed.call(resource)) end 但是不要真正告诉我何时/如何使用.cal

I此方法,其中最后使用
.call

def allow?(controller, action, resource = nil)
  allowed = @allow_all || @allowed_actions[[controller.to_s, action.to_s]]
  allowed && (allowed == true || resource && allowed.call(resource))
end

但是不要真正告诉我何时/如何使用
.call

方法
.call
的目的是调用/执行一个
Proc/method
实例。下面的例子可能会更清楚

m = 12.method("+")
# => `method` gets the `+` method defined in the `Fixnum` instance
# m.class 
# => Method

m.call(3)    #=> 15
# `3` is passed inside the `+` method as argument 
m.call(20)   #=> 32
在上面的示例中,
Fixnum
12定义了方法
+

在您发布的示例中:

def allow?(controller, action, resource = nil)
  allowed = @allow_all || @allowed_actions[[controller.to_s, action.to_s]]
  allowed && (allowed == true || resource && allowed.call(resource))
end
@allowed_actions[[controller.to_s,action.to_s]
返回一个
Proc
实例,
resource
是方法调用的一个
参数

例如:

hash = {[:controller, :action] => 'value'}
# => {[:controller, :action]=>"value"} 

> hash[[:controller,:value]]
# => nil 

> hash[[:controller,:action]]
# => "value" 

FYI:在ruby中,您可以使用
数组
作为
散列
对象的

@allow\u all
包含
true
(如果已设置)。但如果它不是
true
,则包含从(散列?)中检索的过程。当您想调用(即执行)proc时,可以使用
call
。这是命令模式的一个版本,其中“命令”名称是控制器和操作名称的数组。文档看起来很清楚。当问这样的问题时,在提问之前展示你自己在回答这个问题方面做了哪些工作是非常重要的。你读了什么?你试了什么?照目前的情况,很难说你在提出问题之前研究过这个问题。