on-internet
← Community

Ruby object `with` method alternative for Hash keys

Stack Overflow ·

According to the documentation: client.with(timeout: 1) do yield end is equivalent to: old_value = client.timeout begin client.timeout = 1 yield ensure client.timeout = old_value end Is there any equivalent for setting a key on a Hash? my_key = 'something' old_value = my_hash[my_key] begin my_hash[my_key] = 'something else' yield ensure my_hash[my_key] = old_value end

Comments

Sign in or sign up to comment and upvote.

No comments yet.