Project

General

Profile

Actions

Feature #13009

closed

Implement fetch for Thread.current

Added by dwbutler (David Butler) over 7 years ago. Updated about 7 years ago.

Status:
Closed
Target version:
-
[ruby-core:78500]

Description

It's a common idiom to use a fiber-local variable, but return a default value if it hasn't been assigned. Here's an example of Ruby code I would expect to be able to write:

def feature_enabled?
  Thread.current.fetch(:feature_enabled, true)
end

Unfortunately, Thread.current does not implement fetch, like Hash does. So I have to write the more verbose:

def feature_enabled?
  Thread.current.key?(:feature_enabled) ? Thread.current[:feature_enabled] : true
end

Thanks for your consideration!

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0