diff --git a/lib/twig/extension/core.rb b/lib/twig/extension/core.rb index 6c83e3d..2fc9b5f 100644 --- a/lib/twig/extension/core.rb +++ b/lib/twig/extension/core.rb @@ -869,7 +869,7 @@ def self.get_attribute( if object.respond_to?(:[]) && ( (object.is_a?(Array) && attribute.is_a?(Integer) && attribute < object.length) || ( - object.is_a?(Hash) && ( + object.respond_to?(:key?) && ( object.key?(attribute) || (attribute.respond_to?(:to_sym) && object.key?(attribute.to_sym)) || (attribute.respond_to?(:to_s) && object.key?(attribute.to_s)) diff --git a/spec/lib/twig/extension/core_spec.rb b/spec/lib/twig/extension/core_spec.rb index 9022f09..df31e59 100644 --- a/spec/lib/twig/extension/core_spec.rb +++ b/spec/lib/twig/extension/core_spec.rb @@ -504,5 +504,19 @@ def foo ) ).to eq(42) end + + it 'uses bracket access even when not a hash or array if doing an array call' do + object = Class.new(SimpleDelegator).new({ pi: 3.14 }) + + expect( + described_class.get_attribute( + environment, + instance_double(Twig::Source), + object, + :pi, + Twig::Template::METHOD_CALL + ) + ).to eq(3.14) + end end end diff --git a/twig-ruby.gemspec b/twig-ruby.gemspec index 716b335..3788f76 100644 --- a/twig-ruby.gemspec +++ b/twig-ruby.gemspec @@ -2,7 +2,7 @@ Gem::Specification.new do |s| s.name = 'twig_ruby' - s.version = '0.0.7' + s.version = '0.0.8' s.summary = 'Twig Templating for Ruby' s.description = '' s.authors = ['Craig Blanchette', 'Fabian Potencier']