惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
Last Week in AI
Last Week in AI
Blog — PlanetScale
Blog — PlanetScale
V
Visual Studio Blog
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
博客园 - Franky
IT之家
IT之家
博客园 - 叶小钗
Engineering at Meta
Engineering at Meta
The GitHub Blog
The GitHub Blog
雷峰网
雷峰网
腾讯CDC
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
人人都是产品经理
人人都是产品经理
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
博客园_首页
G
Google Developers Blog

Codebase Audits & Rescue | Ally Piechowski

Most of your tech debt is free Your Setup Script Should Support Git Worktrees How to Be a Good Open Source Maintainer "The Git Commands I Run Before Reading Any Code" "Ruby 3.2 Is EOL: What You Actually Need to Do" "Rails 7.2 to 8.1 Upgrade: What Actually Breaks and How to Fix It" "Why Your Engineering Team Is Slow (It's the Codebase, Not the People)" "Migrating from Sprockets to Propshaft: Is It Worth It?" "How to Close a Tab in Vim" "How I Audit a Legacy Rails Codebase in the First Week" "How to Open a New Tab in Vim" "Rails default_scope: Why You Should Never Use It" "Solved: ActionController::ParameterMissing (param is missing or the value is empty)" "Vim: How to Open Current Opened File in New Tab" "Rails: How to Use Greater Than/Less Than in Active Record where Statements" "What is the best time for stand-up meetings?" "Using Let and Context to Modularize RSpec Tests" "What Is Fed vs Unfed Sourdough Starter?" "My Father, My Mentor, My Teacher" "How to Get Over Burnout" "What's the difference between a Good Developer and a Good Googler?" Codebase Audits & Rescue | Ally Piechowski
"Solved: Warning: Using the last argument as keyword para...
"Ally Piechowski" · 2020-01-03 · via Codebase Audits & Rescue | Ally Piechowski

Ruby 2.7 deprecated the use of hashes in the last argument of a method call. Fix this warning by removing the curly braces or adding a hash splat (**) to a variable argument.

Ally Piechowski · · Updated · 2 min read
Solved: Warning: Using the last argument as keyword parameters is deprecated
warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
warning: The called method `test' is defined here

Ruby 2.7 deprecated the use of hashes in the last argument of a method call. Luckily, this can be resolved by removing the curly braces of a direct hash argument, or adding a hash splat ** to a variable argument

For these examples, we’ll be using the following sample function to demonstrate the warnings:

def hello(name: 'World')
  "Hello, #{name}!"
end

Plain Hash Argument

When a hash is the last argument in a method and the method expects keyword arguments, then Ruby automatically converts this argument into keyword arguments.

Solution

To remove the warning, we can simply remove the curly braces. This solution works for any number of keywords.

Variable Hash Argument

Likewise, when a hash is passed as a variable in the final argument slot, it is also converted into keyword arguments. Since this hash could be coming from elsewhere in the application, it isn’t always as easy as moving the hash into the method parameters and removing curly braces…

details = { name: 'Alex' }
hello(details)

Solution

Luckily, Ruby has a feature called a “hash splat” (**). When a hash splat is applied on a hash, it expands the hash’s keys into multiple arguments, allowing this error to be dissipated as the method receives keyword arguments rather than receiving a hash.

details = { name: 'Alex' }
hello(**details)

If we want to suppress this warning, we can do this via an environment variable.

Solution: Export Environment Variable

After executing export RUBYOPT='-W:no-deprecated', any Ruby command will ignore deprecation notices. This sets an environment variable telling Ruby to ignore deprecation notices.

If instead, you want to ignore notices per command, you must use the inline environment variable syntax, RUBYOPT='-W:no-deprecated' ruby file.rb.

For instance, for a Rails app, you would use RUBYOPT='-W:no-deprecated' rails server


Related Articles