summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsazor <sazor.mail@gmail.com>2016-10-17 13:58:18 +0300
committersazor <sazor.mail@gmail.com>2016-10-17 15:40:04 +0300
commit03cd78d7349b1aa137d87c5e2095f8d0856f4c98 (patch)
tree939d7b38a29e75e0ef8f844eed8cf55dc9bf49a9
parent2f740ebbceda2a3bf0597f046564921d3c48c326 (diff)
downloadhashie-03cd78d7349b1aa137d87c5e2095f8d0856f4c98.tar.gz
Fix dig bug for Array
-rw-r--r--CHANGELOG.md1
-rw-r--r--lib/hashie/array.rb9
-rw-r--r--spec/hashie/array_spec.rb12
3 files changed, 21 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index af9ddc3..8aad100 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -29,6 +29,7 @@ scheme are considered to be bugs.
### Fixed
* [#369](https://github.com/intridea/hashie/pull/369): If a translation for a property exists when using IndifferentAccess and IgnoreUndeclared, use the translation to find the property - [@whitethunder](https://github.com/whitethunder).
+* [#376](https://github.com/intridea/hashie/pull/376): Leave string index unchanged if it can't be converted to integer for Array#dig - [@sazor](https://github.com/sazor).
### Security
diff --git a/lib/hashie/array.rb b/lib/hashie/array.rb
index c615813..7e237a4 100644
--- a/lib/hashie/array.rb
+++ b/lib/hashie/array.rb
@@ -7,7 +7,14 @@ module Hashie
include Hashie::Extensions::RubyVersionCheck
with_minimum_ruby('2.3.0') do
def dig(*indexes)
- super(*indexes.map { |idx| Integer(idx) })
+ converted_indexes = indexes.map do |idx|
+ begin
+ Integer(idx)
+ rescue ArgumentError
+ idx
+ end
+ end
+ super(*converted_indexes)
end
end
end
diff --git a/spec/hashie/array_spec.rb b/spec/hashie/array_spec.rb
index 6c20a83..3aba1fb 100644
--- a/spec/hashie/array_spec.rb
+++ b/spec/hashie/array_spec.rb
@@ -12,6 +12,18 @@ describe Array do
it 'works with a numeric index' do
expect(array.dig(1)).to eq(:b)
end
+
+ context 'when array is empty' do
+ let(:array) { Hashie::Array.new([]) }
+
+ it 'works with a first numeric and next string index' do
+ expect(array.dig(0, 'hello')).to eq(nil)
+ end
+
+ it 'throws an error with first string and next numeric index' do
+ expect { array.dig('hello', 0) }.to raise_error(TypeError)
+ end
+ end
end
end
end