From 6d9c0f255a8b571e5839fa8eefe285611b53ac32 Mon Sep 17 00:00:00 2001 From: tompng Date: Wed, 9 Oct 2024 13:31:48 +0900 Subject: [PATCH 1/2] Fix pretty printing range begin/end with false or nil --- lib/pp.rb | 5 +++-- test/test_pp.rb | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/pp.rb b/lib/pp.rb index ed7e899..63c4780 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -470,11 +470,12 @@ def pretty_print_cycle(q) # :nodoc: class Range # :nodoc: def pretty_print(q) # :nodoc: - q.pp self.begin if self.begin + both_nil = self.begin == nil && self.end == nil + q.pp self.begin if self.begin != nil || both_nil q.breakable '' q.text(self.exclude_end? ? '...' : '..') q.breakable '' - q.pp self.end if self.end + q.pp self.end if self.end != nil || both_nil end end diff --git a/test/test_pp.rb b/test/test_pp.rb index 57a3876..a180ee2 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -34,6 +34,10 @@ def test_range assert_equal("0...1\n", PP.pp(0...1, "".dup)) assert_equal("0...\n", PP.pp(0..., "".dup)) assert_equal("...1\n", PP.pp(...1, "".dup)) + assert_equal("..false\n", PP.pp(..false, "".dup)) + assert_equal("false..\n", PP.pp(false.., "".dup)) + assert_equal("false..false\n", PP.pp(false..false, "".dup)) + assert_equal("nil..nil\n", PP.pp(nil..nil, "".dup)) end end From 3e4b7c03b087b9f6c3df46ba9c3aadf485d82d81 Mon Sep 17 00:00:00 2001 From: tomoya ishida Date: Wed, 13 Nov 2024 23:18:41 +0900 Subject: [PATCH 2/2] Simplify range nil check Co-authored-by: Nobuyoshi Nakada --- lib/pp.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/pp.rb b/lib/pp.rb index 63c4780..5721d72 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -470,12 +470,13 @@ def pretty_print_cycle(q) # :nodoc: class Range # :nodoc: def pretty_print(q) # :nodoc: - both_nil = self.begin == nil && self.end == nil - q.pp self.begin if self.begin != nil || both_nil + begin_nil = self.begin == nil + end_nil = self.end == nil + q.pp self.begin if !begin_nil || end_nil q.breakable '' q.text(self.exclude_end? ? '...' : '..') q.breakable '' - q.pp self.end if self.end != nil || both_nil + q.pp self.end if !end_nil || begin_nil end end