Skip to content

Commit

Permalink
Use OpenStruct only if available (#562)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yahonda committed Apr 29, 2024
1 parent 3875613 commit 0919e3f
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/jbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand Down

0 comments on commit 0919e3f

Please sign in to comment.