[#78633] ruby/spec needs help from CRuby committers — Benoit Daloze <eregontp@...>

Currently, ruby/spec is maintained mostly by individuals and enjoys the

13 messages 2016/12/13

[ruby-core:78650] [Ruby trunk Feature#13009] Implement fetch for Thread.current

From: ko1@...
Date: 2016-12-15 05:40:44 UTC
List: ruby-core #78650
Issue #13009 has been updated by Koichi Sasada.

Assignee set to Nobuyoshi Nakada

2.4? 2.5?

----------------------------------------
Feature #13009: Implement fetch for Thread.current
https://bugs.ruby-lang.org/issues/13009#change-62033

* Author: David Butler
* Status: Open
* Priority: Normal
* Assignee: Nobuyoshi Nakada
* Target version: 
----------------------------------------
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:

~~~ ruby
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:

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

Thanks for your consideration!




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next