Skip to content

Commit 5dcdd37

Browse files
authored
Mention Hash#fetch_values (#936)
1 parent 378e9d0 commit 5dcdd37

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

README.adoc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,7 +2297,7 @@ Prefer the use of exceptions from the standard library over introducing new exce
22972297
=== Parallel Assignment [[parallel-assignment]]
22982298

22992299
Avoid the use of parallel assignment for defining variables.
2300-
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.
2300+
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.
23012301
Parallel assignment is less readable than separate assignment.
23022302

23032303
[source,ruby]
@@ -4404,18 +4404,25 @@ batman.fetch(:powers, obtain_batman_powers) # obtain_batman_powers is an expensi
44044404
batman.fetch(:powers) { obtain_batman_powers }
44054405
----
44064406

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

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

44114411
[source,ruby]
44124412
----
44134413
# bad
44144414
email = data['email']
44154415
username = data['nickname']
44164416
4417+
# bad
4418+
keys = %w[email nickname].freeze
4419+
email, username = keys.map { |key| data[key] }
4420+
44174421
# good
44184422
email, username = data.values_at('email', 'nickname')
4423+
4424+
# also good
4425+
email, username = data.fetch_values('email', 'nickname')
44194426
----
44204427

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

0 commit comments

Comments
 (0)