- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: ruby-best-practices/hash-each
Language: Ruby
Severity: Notice
Category: Best Practices
The rule “Prefer using hash each_key and each_value” recommends using the specific iterator methods each_key
and each_value
when iterating over the keys or values of a hash. It is considered a best practice in Ruby to use these methods instead of the more general each
method followed by keys
or values
.
This rule is important because it helps to improve the readability and clarity of your code. When you use each_key
or each_value
, it’s immediately clear that you’re working with either the keys or the values of the hash. This makes your code easier to understand and maintain.
To follow this rule, you should replace any instances of hash.keys.each
with hash.each_key
, and hash.values.each
with hash.each_value
. This will make your code more idiomatic and easier to read, while still achieving the same functionality.
# Updates the method
hash.keys.each { |k| p k }
hash.keys.each do |k| p k end
hash.values.each { |v| p v }
hash.values.each do |v| p v end
# Updates the method and parameters
hash.each { |k, _v| p k }
hash.each do |k, _v| p k end
hash.each { |_k, v| p v }
hash.each do |_k, v| p v end
hash.each_key { |k| p k }
hash.each_value { |v| p v }
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products