Skip to content

Commit 503511d

Browse files
transclaude
andcommitted
Review Hash methods: deprecate redundant, keep unique
- Deprecate Hash#update_keys (use transform_keys! or rekey!) - Deprecate Hash#update_values (use transform_values! or revalue!) - Deprecate Hash#fetch_nested (use Hash#dig, Ruby 2.3+) - Keep rekey/revalue as more powerful alternatives to transform_keys/values - Keep symbolize_keys/stringify_keys (ActiveSupport compatibility) - Keep all other Hash methods (40 unique utilities) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 468acc4 commit 503511d

4 files changed

Lines changed: 19 additions & 30 deletions

File tree

HISTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ Changes:
9090
* Add `Dir.find` as convenience wrapper around Ruby's `Find.find`.
9191
* Deprecate `Dir.recurse` / `Dir.ls_r` (use `Dir.find` or `Find.find` instead).
9292
* Deprecate `Proc#compose` and `Proc#*` (use `Proc#<<` for right-to-left composition, Ruby 2.6+).
93+
* Deprecate `Hash#update_keys` (use `Hash#transform_keys!` or `Hash#rekey!`).
94+
* Deprecate `Hash#update_values` (use `Hash#transform_values!` or `Hash#revalue!`).
95+
* Deprecate `Hash#fetch_nested` (use `Hash#dig`, adopted by Ruby in 2.3).
9396
* Fix dead requires for removed `kernel/singleton_class` in Proc and Kernel.
9497
* Remove misplaced `applique/file_helpers` from core (test infrastructure).
9598
* Drop unused `test_files` directive from gemspec. (PR#301)

lib/core/facets/hash/fetch_nested.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
class Hash
22

3-
# Similar to Hash#fetch but supports nested lookup and is `nil` safe.
3+
# @deprecated Use Hash#dig instead (built-in since Ruby 2.3).
44
#
5-
# {}.fetch_nested('anything','at','all') #=> nil
5+
# Similar to Hash#dig but supports a block for missing keys,
6+
# like Hash#fetch does. Ruby's #dig does not support a block.
67
#
7-
# h = {'hello'=>{'world'=>42}}
8-
# h.fetch_nested(*['hello','world']) #=> 42
8+
# TODO: Consider proposing block support for Hash#dig to Ruby core.
99
#
10-
# CREDIT: T. Yamada and Sean Mackesey
11-
12-
def fetch_nested(*keys)
10+
def fetch_nested(*keys, &block)
11+
warn "Hash#fetch_nested is deprecated. Use Hash#dig instead.", uplevel: 1
1312
begin
14-
keys.reduce(self){|accum, k| accum.fetch(k)}
15-
rescue (RUBY_VERSION<'1.9' ? IndexError : KeyError)
13+
keys.reduce(self) { |accum, k| accum.fetch(k) }
14+
rescue KeyError
1615
block_given? ? yield(*keys) : nil
1716
end
1817
end
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
class Hash
22

3-
# Iterate over hash updating just the keys.
3+
# @deprecated Use Hash#transform_keys! (Ruby 2.5+) or Hash#rekey! instead.
44
#
5-
# h = {:a=>1, :b=>2}
6-
# h.update_keys{ |k| "#{k}!" }
7-
# h #=> { "a!"=>1, "b!"=>2 }
8-
#
9-
# @author Trans
10-
# @author Evgeniy Dolzhenko (bug fix)
11-
12-
def update_keys #:yield:
5+
def update_keys(&block)
6+
warn "Hash#update_keys is deprecated. Use Hash#transform_keys! or Hash#rekey! instead.", uplevel: 1
137
if block_given?
14-
keys.each { |old_key| store(yield(old_key), delete(old_key)) }
8+
transform_keys!(&block)
159
else
1610
to_enum(:update_keys)
1711
end
1812
end
1913

2014
end
21-
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
class Hash
22

3-
# Iterate over hash updating just the values.
3+
# @deprecated Use Hash#transform_values! (Ruby 2.4+) or Hash#revalue! instead.
44
#
5-
# h = {:a=>1, :b=>2}
6-
# h.update_values{ |v| v + 1 }
7-
# h #=> { :a=>2, :b=>3 }
8-
#
9-
# CREDIT: Trans
10-
11-
def update_values #:yield:
5+
def update_values(&block)
6+
warn "Hash#update_values is deprecated. Use Hash#transform_values! or Hash#revalue! instead.", uplevel: 1
127
if block_given?
13-
each{ |k,v| store(k, yield(v)) }
8+
transform_values!(&block)
149
else
1510
to_enum(:update_values)
1611
end
1712
end
1813

1914
end
20-

0 commit comments

Comments
 (0)