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

Fix conflict between Hash#values_at and "Parallel Assignment" #936

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2297,7 +2297,7 @@ Prefer the use of exceptions from the standard library over introducing new exce
=== Parallel Assignment [[parallel-assignment]]

Avoid the use of parallel assignment for defining variables.
Parallel assignment is allowed when it is the return of a method call, used with the splat operator, or when used to swap variable assignment.
Parallel assignment is allowed when it is the return of a method call (e.g. `Hash#values_at`), used with the splat operator, or when used to swap variable assignment.
Parallel assignment is less readable than separate assignment.

[source,ruby]
Expand Down Expand Up @@ -4404,18 +4404,25 @@ batman.fetch(:powers, obtain_batman_powers) # obtain_batman_powers is an expensi
batman.fetch(:powers) { obtain_batman_powers }
----

=== `Hash#values_at` [[hash-values-at]]
=== `Hash#values_at` and `Hash#fetch_values` [[hash-values-at-and-hash-fetch-values]]

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

[source,ruby]
----
# 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')

# also good
email, username = data.fetch_values('email', 'nickname')
----

=== `Hash#transform_keys` and `Hash#transform_values` [[hash-transform-methods]]
Expand Down