Sha256: 341a6addc059522fb428e7cd638d6f01c4f57295c8ce831eefcea4032c0f2988

Contents?: true

Size: 1.3 KB

Versions: 6770

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # This cop checks for `OpenStruct.new` calls.
      # Instantiation of an `OpenStruct` invalidates
      # Ruby global method cache as it causes dynamic method
      # definition during program runtime.
      # This could have an effect on performance,
      # especially in case of single-threaded
      # applications with multiple `OpenStruct` instantiations.
      #
      # @example
      #   # bad
      #   class MyClass
      #     def my_method
      #       OpenStruct.new(my_key1: 'my_value1', my_key2: 'my_value2')
      #     end
      #   end
      #
      #   # good
      #   class MyClass
      #     MyStruct = Struct.new(:my_key1, :my_key2)
      #     def my_method
      #       MyStruct.new('my_value1', 'my_value2')
      #     end
      #   end
      #
      class OpenStruct < Cop
        MSG = 'Consider using `Struct` over `OpenStruct` ' \
              'to optimize the performance.'.freeze

        def_node_matcher :open_struct, <<-PATTERN
          (send (const {nil? cbase} :OpenStruct) :new ...)
        PATTERN

        def on_send(node)
          open_struct(node) do |method|
            add_offense(node, location: :selector, message: format(MSG, method))
          end
        end
      end
    end
  end
end

Version data entries

6,770 entries across 6,766 versions & 24 rubygems

Version Path
cybrid_api_bank_ruby-0.123.149 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
cybrid_api_id_ruby-0.123.149 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
cybrid_api_organization_ruby-0.123.149 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
cybrid_api_bank_ruby-0.123.148 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
cybrid_api_id_ruby-0.123.148 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
cybrid_api_organization_ruby-0.123.148 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
cybrid_api_bank_ruby-0.123.147 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
cybrid_api_organization_ruby-0.123.147 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
cybrid_api_id_ruby-0.123.147 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
ory-client-1.16.2 vendor/bundle/ruby/3.1.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
avalara_sdk-24.12.2 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
cybrid_api_bank_ruby-0.123.145 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
cybrid_api_organization_ruby-0.123.145 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
cybrid_api_id_ruby-0.123.145 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
cybrid_api_bank_ruby-0.123.144 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
cybrid_api_id_ruby-0.123.144 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
cybrid_api_organization_ruby-0.123.144 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
ory-client-1.16.1 vendor/bundle/ruby/3.1.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
ory-client-1.16.0 vendor/bundle/ruby/3.1.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb
cybrid_api_bank_ruby-0.123.143 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/open_struct.rb