Sha256: 763d169274c80dec6f6d79e9b4bd9394411207a4b773849577ceb8ba0e27eefe

Contents?: true

Size: 811 Bytes

Versions: 15

Compression:

Stored size: 811 Bytes

Contents

if RUBY_VERSION >= '2.0.0' && (Struct.new(:a, :keyword_init => true) && false rescue true)
  require 'backports/tools/alias_method_chain'
  eval %q[
  class << Struct
    def new_with_keyword_init(*members, keyword_init: false, &block)
      klass = new_without_keyword_init(*members)
      if keyword_init
        members.shift unless members.first.is_a?(Symbol)
        arg_list = members.map { |m| "#{m}: nil"}.join(', ')
        setter = members.map { |m| "self.#{m} = #{m} " }.join("\n")
        klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def initialize(#{arg_list})
            #{setter}
          end
        RUBY
      end
      klass.class_eval(&block) if block
      klass
    end
    Backports.alias_method_chain(self, :new, :keyword_init)
  end
  ], nil, __FILE__, __LINE__ - 19
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
backports-3.25.0 lib/backports/2.5.0/struct/new.rb
backports-3.24.1 lib/backports/2.5.0/struct/new.rb
backports-3.24.0 lib/backports/2.5.0/struct/new.rb
backports-3.23.0 lib/backports/2.5.0/struct/new.rb
backports-3.22.1 lib/backports/2.5.0/struct/new.rb
backports-3.22.0 lib/backports/2.5.0/struct/new.rb
backports-3.21.0 lib/backports/2.5.0/struct/new.rb
backports-3.20.2 lib/backports/2.5.0/struct/new.rb
backports-3.20.1 lib/backports/2.5.0/struct/new.rb
backports-3.20.0 lib/backports/2.5.0/struct/new.rb
backports-3.19.0 lib/backports/2.5.0/struct/new.rb
backports-3.18.2 lib/backports/2.5.0/struct/new.rb
backports-3.18.1 lib/backports/2.5.0/struct/new.rb
backports-3.18.0 lib/backports/2.5.0/struct/new.rb
backports-3.17.2 lib/backports/2.5.0/struct/new.rb