diff --git a/lib/dry/struct.rb b/lib/dry/struct.rb index 491e6ba..1043201 100644 --- a/lib/dry/struct.rb +++ b/lib/dry/struct.rb @@ -134,7 +134,11 @@ def initialize(attributes) # rom_n_roda[:title] #=> 'Web Development with ROM and Roda' # rom_n_roda[:subtitle] #=> nil def [](name) - @attributes.fetch(name) { raise MissingAttributeError, name } + @attributes.fetch(name) do + next if self.class.attribute_names.include?(name) + + raise MissingAttributeError, name + end end # Converts the {Dry::Struct} to a hash with keys representing diff --git a/spec/integration/attributes_from_spec.rb b/spec/integration/attributes_from_spec.rb index b0af47a..79e48ab 100644 --- a/spec/integration/attributes_from_spec.rb +++ b/spec/integration/attributes_from_spec.rb @@ -129,6 +129,7 @@ class Person < Dry::Struct it "adds omittable keys" do expect(person_without_country.country).to be_nil + expect(person_without_country[:country]).to be_nil expect(person_with_country.country).to eql("uk") end end