gpt4 book ai didi

ruby-on-rails - 类型错误 : no implicit conversion of Symbol into Integer while reading YAML in Ruby

转载 作者:数据小太阳 更新时间:2023-10-29 08:09:04 24 4
gpt4 key购买 nike

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

module Selecting
module Execution
class ExecuteSpecific

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

为了使代码更通用,我想从函数中删除特定的方法调用并将其模式化为 YAML 文件:

:Newname:
- example: 'Data::FetchData.new.perform_action(param1, params2)'

并将上述重构为(“名称”应作为符号传递):

module Selecting
module Execution
class ExecuteSpecific

def self.perform! name

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

这返回一个

TypeError: no implicit conversion of Symbol into Integer

如何解决?

最佳答案

那个错误是说你在需要整数的地方使用了一个符号。您唯一要做的就是调用 new[:example] 的地方。 Yaml.load_file 返回字符串数组而不是符号,因此如果您使用字符串索引而不是符号访问您的 yaml 加载文档,应该可以解决问题。

例如。 input = new['example']

关于ruby-on-rails - 类型错误 : no implicit conversion of Symbol into Integer while reading YAML in Ruby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20302394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com