Sha256: ff78a69c08b87357bbb669d918bdf603f98b5491aebd647d22e80bde7ba58f88

Contents?: true

Size: 1.08 KB

Versions: 6

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

# Source: https://github.com/ruby/ruby/blob/b76a21aa45fff75909a66f8b20fc5856705f7862/struct.c#L953-L980
RubyNext::Core.patch Struct, method: :deconstruct_keys, version: "2.7" do
  <<-'RUBY'
def deconstruct_keys(keys)
  raise TypeError, "wrong argument type #{keys.class} (expected Array or nil)" if keys && !keys.is_a?(Array)

  return to_h unless keys

  keys.each_with_object({}) do |k, acc|
    # if k is Symbol and not a member of a Struct return {}
    next if (Symbol === k || String === k) && !members.include?(k.to_sym)
    # if k is Integer check that index is not ouf of bounds
    next if Integer === k && k > size - 1
    acc[k] = self[k]
  end
end
  RUBY
end

# We need to hack `respond_to?` in Ruby 2.5, since it's not working with refinements
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.6")
  RubyNext::Core.patch refineable: Struct, name: "StructRespondToDeconstruct", method: :deconstruct_keys, version: "2.7" do
    <<-RUBY
def respond_to?(mid, *)
  return true if mid == :deconstruct_keys || mid == :deconstruct
  super
end
    RUBY
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ruby-next-core-0.10.5 lib/ruby-next/core/struct/deconstruct_keys.rb
ruby-next-core-0.10.4 lib/ruby-next/core/struct/deconstruct_keys.rb
ruby-next-core-0.10.3 lib/ruby-next/core/struct/deconstruct_keys.rb
ruby-next-core-0.10.2 lib/ruby-next/core/struct/deconstruct_keys.rb
ruby-next-core-0.10.1 lib/ruby-next/core/struct/deconstruct_keys.rb
ruby-next-core-0.10.0 lib/ruby-next/core/struct/deconstruct_keys.rb