我在rails应用程序的模块中定义了以下内容:

module Selecting
  module Execution
    class ExecuteSpecific

      def self.perform!
        input = Data::FetchData.new.perform_action(param1, params2)

为了使代码更通用,我希望从函数中删除特定的methodcall,并将其模式化为yaml文件,如下所示:
:Newname:
  - example: 'Data::FetchData.new.perform_action(param1, params2)'

并将上述内容重构为(“name”应作为符号传递):
module Selecting
  module Execution
    class ExecuteSpecific

      def self.perform! name

        new = YAML.load_file('path/to/file.yml')[name]
        input = new[:example]

这次返回
typeerror:没有将符号隐式转换为整数
怎么能解决呢?

最佳答案

这个错误是说您正在使用一个需要整数的符号。你所做的唯一的事情就是打电话给new[:example]Yaml.load_file返回字符串数组,而不是符号数组,因此如果使用字符串索引而不是符号访问加载了yaml的文档,应该可以解决问题。
input = new['example']

07-24 09:49
查看更多