Skip to content

Commit c407e94

Browse files
committed
Fix CICPHash#to_s to operate like #inspect on Ruby 1.9+
1 parent b7b0e0b commit c407e94

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
=== master
2+
3+
* Fix CICPHash#to_s to operate like #inspect on Ruby 1.9+ (jeremyevans)
4+
15
=== 2.0.0 (2021-12-10)
26

37
* Support new Hash instance methods added in 1.9 - 3.0 (jeremyevans)

cicphash.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,12 @@ def to_hash
260260
hash
261261
end
262262

263-
def to_s
264-
to_a.join
263+
if RUBY_VERSION >= '1.9'
264+
alias to_s inspect
265+
else
266+
def to_s
267+
to_a.join
268+
end
265269
end
266270

267271
def update(hash, &block)

test/test_cicphash.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,24 @@ def test_index
253253
end
254254
end
255255

256-
def test_inspect_and_to_s
256+
def test_inspect
257257
assert_equal '{}', CICPHash[].inspect
258-
assert_equal '', CICPHash[].to_s
259258
assert_equal '{1=>2}', CICPHash[1=>2].inspect
260-
assert_equal '12', CICPHash[1=>2].to_s
261259
assert ['{:ab=>"CD", [:"3"]=>4}', '{[:"3"]=>4, :ab=>"CD"}'].include?(CICPHash[:ab=>'CD', [:'3']=>4].inspect)
262-
assert ['abCD34', '34abCD'].include?(CICPHash[:ab=>'CD', [:'3']=>4].to_s)
260+
end
261+
262+
if RUBY_VERSION >= '1.9'
263+
def test_to_s
264+
assert_equal '{}', CICPHash[].to_s
265+
assert_equal '{1=>2}', CICPHash[1=>2].to_s
266+
assert_equal '{:ab=>"CD", [:"3"]=>4}', CICPHash[:ab=>'CD', [:'3']=>4].to_s
267+
end
268+
else
269+
def test_to_s
270+
assert_equal '', CICPHash[].to_s
271+
assert_equal '12', CICPHash[1=>2].to_s
272+
assert ['abCD34', '34abCD'].include?(CICPHash[:ab=>'CD', [:'3']=>4].to_s)
273+
end
263274
end
264275

265276
def test_invert

0 commit comments

Comments
 (0)