Skip to content

Commit

Permalink
support negative array indexes deeply in get keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Garett Shulman committed Jun 15, 2014
1 parent fdc0d13 commit bdd2b8e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,8 @@ def Map.collection_has?(collection, key, &block)
has_key =
case collection
when Array
key = (Integer(key) rescue -1)
(0...collection.size).include?(key)
key = (Integer(key) rescue nil)
(0...collection.size).include?(key) or (0...collection.size).include?((-key)-1)

when Hash
collection.has_key?(key)
Expand Down
4 changes: 4 additions & 0 deletions test/map_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ def to_map() {:k => :b} end
assert{ m[:x][:y].is_a?(Map) }
assert{ m[:x][:y][:z] == 42.0 }

m = Map.new(a: [b: :c])
assert { m.get(:a, '-1', :b) == m.get(:a, '-1').get(:b) }
assert { m.get(:a, '-1', :b) == :c }

assert{ Map.new.tap{|nm| nm.set} =~ {} }
assert{ Map.new.tap{|nm| nm.set({})} =~ {} }
end
Expand Down

0 comments on commit bdd2b8e

Please sign in to comment.