Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cop idea: Hash#values_at #12336

Open
ydakuka opened this issue Nov 4, 2023 · 3 comments
Open

Cop idea: Hash#values_at #12336

ydakuka opened this issue Nov 4, 2023 · 3 comments

Comments

@ydakuka
Copy link

ydakuka commented Nov 4, 2023

Description

Use Hash#values_at or Hash#fetch_values when you need to retrieve several values consecutively from a hash.

https://rubystyle.guide/#hash-values-at-and-hash-fetch-values

Behavior

# bad
email = data['email']
username = data['nickname']

# bad
keys = %w[email nickname].freeze
email, username = keys.map { |key| data[key] }

# good
email, username = data.values_at('email', 'nickname')

# good
keys = %w[email nickname].freeze
email, username = data.values_at(*keys)
@ydakuka
Copy link
Author

ydakuka commented Nov 4, 2023

I am not sure that this rule does not conflict with the Style/ParallelAssignment cop.

@koic
Copy link
Member

koic commented Nov 22, 2023

I am not sure that this rule does not conflict with the Style/ParallelAssignment cop.

Good point. It may be beneficial to re-review the https://rubystyle.guide/#hash-values-at rule in the style guide again before suggesting the cop implementation. Actually, it conflicts with the https://rubystyle.guide/#parallel-assignment rule as well.

@ydakuka
Copy link
Author

ydakuka commented Nov 22, 2023

Done. rubocop/ruby-style-guide#936

@ydakuka ydakuka changed the title Cop idea: Hash#values_at Cop idea: Hash#values_at Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants