【问题标题】:warning: Using the last argument as keyword parameters is deprecated警告:不推荐使用最后一个参数作为关键字参数
【发布时间】:2021-04-06 11:58:16
【问题描述】:
  json: 1.8.6
  ruby: 2.7.2
  rails: 6.0.3.6

我的应用程序经常出现以下错误

 /Users/***/.rvm/gems/ruby-2.7.2/gems/json-1.8.6/lib/json/common.rb:155: warning: Using the last argument as keyword parameters is deprecated

当我搜索此错误时,我得到了一个解决方案,即我需要更新我的 json gem 版本,但在这里我对 json-1.8.6 版本有许多其他依赖项,所以我无法更新 gem。当我明确更新 common.rb 文件中的代码时。

 def parse(source, opts = {})
    Parser.new(source, opts).parse
 end
 

我已将上面的代码更新如下

def parse(source, opts = {})
  Parser.new(source, **opts).parse
end

它可以正常工作,但我认为这不是禁用警告的正确方法。

【问题讨论】:

  • 添加** 是可以的,但在安装的common.rb 中手动添加是不行的。您确实应该升级 JSON gem 并修复依赖于该特定版本的所有内容,1.8.6 已经很老了,越早更新越好。如果您现在绝对无法更新 gem,那么您最好在 1.8.6 版本中对其进行分叉,修补分叉,然后更新您的 Gemfile 以使用您的分叉版本;不过,这是一个临时的黑客攻击,不是正确的前进方式。

标签: ruby-on-rails json


【解决方案1】:

config/initializers/json.rb

 module JSON
   module_function

     def parse(source, opts = {})
     Parser.new(source, **opts).parse
   end
 end

【讨论】:

  • 仍然有时我会收到警告,例如启动 rails server/console/sidekiq 时。在所有其他情况下,警告都消失了。
【解决方案2】:

尝试更新发出警告的特定 gem。 手动执行 - 检查 gem 网站并更新 gemfile,然后执行bundle update。为我工作!

【讨论】:

    猜你喜欢
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多