Sha256: e0008523667515eba26d08794bc1544e8d6845ee7b01282c0f5619484bb25d9c

Contents?: true

Size: 733 Bytes

Versions: 6

Compression:

Stored size: 733 Bytes

Contents

module Polyfill
  module V2_5
    module Struct
      module ClassMethods
        def new(*args, keyword_init: false)
          obj = super(*args)

          if keyword_init
            attrs = args
            attrs.shift if attrs.first.class != Symbol

            ignore_warnings do
              obj.send(:define_method, :initialize) do |**kwargs|
                invalid_args = kwargs.reject { |k, _| attrs.include?(k) }.keys
                unless invalid_args.empty?
                  raise ArgumentError, "unknown keywords: #{invalid_args.join(', ')}"
                end

                super(*kwargs.values_at(*attrs))
              end
            end
          end

          obj
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
polyfill-1.9.0 lib/polyfill/v2_5/struct.rb
polyfill-1.8.0 lib/polyfill/v2_5/struct.rb
polyfill-1.7.0 lib/polyfill/v2_5/struct.rb
polyfill-1.6.0 lib/polyfill/v2_5/struct.rb
polyfill-1.5.0 lib/polyfill/v2_5/struct.rb
polyfill-1.4.0 lib/polyfill/v2_5/struct.rb