Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
Metadata
ID:ruby-best-practices/proc-over-procnew
Language: Ruby
Severity: Info
Category: Best Practices
Description
The rule “Prefer proc over Proc.new” is an important guideline in Ruby programming. It advises developers to use the proc method rather than Proc.new when creating a new Proc object. The proc method is more idiomatic to Ruby and is preferred because of its simplicity and readability.
The importance of this rule lies in maintaining consistency and clarity in your code. Using proc instead of Proc.new makes your code more concise and easier to read and understand. It also aligns with the Ruby community’s best practices, which favor simplicity and readability.
To avoid violating this rule, always use proc when you need to create a new Proc object. For example, instead of writing Proc.new { |n| puts n }, you should write proc { |n| puts n }. This small change can significantly improve the readability of your code.
Non-Compliant Code Examples
p=Proc.new{|n|putsn}
Compliant Code Examples
p=proc{|n|putsn}
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- ruby-best-practices # Rules to enforce Ruby best practices.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines