From 02a3f0ab34aa06d014821c0a6988bfac0b6dd471 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Tue, 16 Apr 2024 21:11:55 +0900 Subject: [PATCH] Use OpenStruct only if available This commit uses OpenStruct only if available because of the following reasons: - Starting from Ruby 3.4.0dev, Using `ostruct` raises the following warning. > ostruct was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.5.0. Add ostruct to your Gemfile or gemspec. - And when the warning category is `:performance` it also raises this warning. > "OpenStruct use is discouraged for performance reasons Refer to https://bugs.ruby-lang.org/issues/20309 https://github.com/ruby/ruby/pull/10428 https://github.com/ruby/ostruct/pull/56 Fix #561 --- lib/jbuilder.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/jbuilder.rb b/lib/jbuilder.rb index 3cf41d8..b6258c7 100644 --- a/lib/jbuilder.rb +++ b/lib/jbuilder.rb @@ -4,8 +4,11 @@ require 'jbuilder/key_formatter' require 'jbuilder/errors' require 'json' -require 'ostruct' require 'active_support/core_ext/hash/deep_merge' +begin + require 'ostruct' +rescue LoadError +end class Jbuilder @@key_formatter = nil @@ -28,7 +31,7 @@ def self.encode(*args, &block) end BLANK = Blank.new - NON_ENUMERABLES = [ ::Struct, ::OpenStruct ].to_set + NON_ENUMERABLES = defined?(::OpenStruct) ? [::Struct, ::OpenStruct].to_set : [::Struct].to_set def set!(key, value = BLANK, *args, &block) result = if ::Kernel.block_given?