Skip to content

Commit b28a17b

Browse files
committed
🔧 Add Config#with(**attrs) to make updated copies
1 parent 9705844 commit b28a17b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

lib/net/imap/config.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,23 @@ def update(**attrs)
161161
self
162162
end
163163

164+
# :call-seq:
165+
# with(**attrs) -> config
166+
# with(**attrs) {|config| } -> result
167+
#
168+
# Returns a shallow copy of +self+. When provided with a block, yields
169+
# the copy to the block and returns the block's result.
170+
#
171+
# If any keyword arguments are given, the copy will be updated with their
172+
# values.
173+
#
174+
# If +self+ is frozen, the copy will also be frozen.
175+
def with(**attrs)
176+
copy = dup.update(**attrs)
177+
copy.freeze if frozen?
178+
block_given? ? yield(copy) : copy
179+
end
180+
164181
# :call-seq: to_h -> hash
165182
#
166183
# Returns all config attributes in a hash.

0 commit comments

Comments
 (0)