summaryrefslogtreecommitdiff
path: root/lib/ostruct.rb
diff options
context:
space:
mode:
authorJean byroot Boussier <[email protected]>2023-09-29 08:48:46 +0200
committergit <[email protected]>2023-09-29 06:48:55 +0000
commitf14cee39b867e416b974856171b52f725cb4892a (patch)
tree65da333ecde98e4c6e2007f399afd544790d7de8 /lib/ostruct.rb
parentb9bf419aa36e438760fda4109561358bb20d09f1 (diff)
[ruby/ostruct] Emit a performance warning when OpenStruct is used
(https://github.com/ruby/ostruct/pull/56) The OpenStruct documentation clearly state that it shouldn't be used when performance is expected. Ruby 3.3 introduce a new category of warnings that is silenced by default: performance. The expected use case is to enable this warning when looking for potential performance issues within an application. As such I think it would make sense to emit a performance warning when OpenStruct is used, as it may help pinpoint that a dependency rely on it, etc. https://github.com/ruby/ostruct/commit/5826e12db8 Co-authored-by: Jean Boussier <[email protected]>
Diffstat (limited to 'lib/ostruct.rb')
-rw-r--r--lib/ostruct.rb12
1 files changed, 12 insertions, 0 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index a08561d6c9..7311138fbf 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -109,6 +109,14 @@
class OpenStruct
VERSION = "0.5.5"
+ HAS_PERFORMANCE_WARNINGS = begin
+ Warning[:performance]
+ true
+ rescue NoMethodError, ArgumentError
+ false
+ end
+ private_constant :HAS_PERFORMANCE_WARNINGS
+
#
# Creates a new OpenStruct object. By default, the resulting OpenStruct
# object will have no attributes.
@@ -124,6 +132,10 @@ class OpenStruct
# data # => #<OpenStruct country="Australia", capital="Canberra">
#
def initialize(hash=nil)
+ if HAS_PERFORMANCE_WARNINGS && Warning[:performance]
+ warn "OpenStruct use is discouraged for performance reasons", uplevel: 1, category: :performance
+ end
+
if hash
update_to_values!(hash)
else