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

推荐订阅源

S
SegmentFault 最新的问题
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
博客园_首页
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
P
Proofpoint News Feed
博客园 - 司徒正美
有赞技术团队
有赞技术团队
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | 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