Skip to content

Commit

Permalink
Stop recursive hash breaking on to_s/inspect (fixes #222)
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Aug 2, 2013
1 parent 1640269 commit e865f62
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions corelib/opal/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,13 @@ def inspect
var inspect = [], keys = #{self}.keys, map = #{self}.map;
for (var i = 0, length = keys.length; i < length; i++) {
var key = keys[i];
inspect.push(#{`key`.inspect} + '=>' + #{`map[key]`.inspect});
var key = keys[i], val = map[key];
if (val === #{self}) {
inspect.push(#{`key`.inspect} + '=>' + '{...}');
} else {
inspect.push(#{`key`.inspect} + '=>' + #{`map[key]`.inspect});
}
}
return '{' + inspect.join(', ') + '}';
Expand Down
9 changes: 9 additions & 0 deletions spec/opal/hash/to_s_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'spec_helper'

describe "Hash#to_s" do
it "handles recursive arrays without breaking" do
h = {}
h['foo'] = h
h.to_s.should == '{"foo"=>{...}}'
end
end

0 comments on commit e865f62

Please sign in to comment.