Skip to content

Commit 0919e3f

Browse files
authored
Use OpenStruct only if available (#562)
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 ruby/ruby#10428 ruby/ostruct#56 Fix #561
1 parent 3875613 commit 0919e3f

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

lib/jbuilder.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
require 'jbuilder/key_formatter'
55
require 'jbuilder/errors'
66
require 'json'
7-
require 'ostruct'
87
require 'active_support/core_ext/hash/deep_merge'
8+
begin
9+
require 'ostruct'
10+
rescue LoadError
11+
end
912

1013
class Jbuilder
1114
@@key_formatter = nil
@@ -28,7 +31,7 @@ def self.encode(*args, &block)
2831
end
2932

3033
BLANK = Blank.new
31-
NON_ENUMERABLES = [ ::Struct, ::OpenStruct ].to_set
34+
NON_ENUMERABLES = defined?(::OpenStruct) ? [::Struct, ::OpenStruct].to_set : [::Struct].to_set
3235

3336
def set!(key, value = BLANK, *args, &block)
3437
result = if ::Kernel.block_given?

0 commit comments

Comments
 (0)