首页 > 解决方案 > ruby 中未定义的局部变量或方法“action_name”错误

问题描述

我在基类中有两个方法,method_1 通过调用worker 执行带有一些*args 的perform_async,工作完成后它将调用method_2,如何将相同的args 传递给method_2?

def self.method_1(action_name, *args)
   perform_async(action_name,args)
   method_2
end

def self.method_2
   some action here
   method_1(action_name, args)
   handle exception here
end

在方法 2 的第 3 行出现错误未定义的局部变量或方法“action_name”。

标签: rubyparametersarguments

解决方案


如果我理解您的目的,那么使用实例变量来存储 splat 参数参数怎么样?

def self.method_1(action_name, *args)
   @args = args
   perform_async(action_name, args)
   method_2
end

def self.method_2
   some action here
   method_1(action_name, @args)
   handle exception here
end

推荐阅读