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

推荐订阅源

L
LangChain Blog
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Stack Overflow Blog
Stack Overflow Blog
Forbes - Security
Forbes - Security
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
S
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
N
News | PayPal Newsroom
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Project Zero
Project Zero
Cyberwarzone
Cyberwarzone
Vercel News
Vercel News
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
Google Online Security Blog
Google Online Security Blog
Simon Willison's Weblog
Simon Willison's Weblog
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
T
Tenable Blog
I
InfoQ
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
D
DataBreaches.Net
博客园 - Franky
Know Your Adversary
Know Your Adversary
Spread Privacy
Spread Privacy
S
Security @ Cisco Blogs
Hacker News: Ask HN
Hacker News: Ask HN
J
Java Code Geeks
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Full Disclosure
C
Cisco Blogs
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Cloudflare Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
I
Intezer
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
F
Fortinet All Blogs
Jina AI
Jina AI
V
Visual Studio Blog
L
LINUX DO - 热门话题
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏

Codebase Audits & Rescue | Ally Piechowski

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 parameters is deprecated"
"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