From 4fd8f4e0bb51a9651b69ad57636f8d86c710a7de Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sat, 4 Oct 2025 15:30:47 +0200 Subject: [PATCH] Fix ::Data warning on Ruby 2.7 * It was showing on require 'pp': lib/pp.rb:525: warning: constant ::Data is deprecated * Fixes https://github.com/ruby/pp/issues/51 --- lib/pp.rb | 9 ++++++++- test/test_pp.rb | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/pp.rb b/lib/pp.rb index eb9f80d..f10fe7f 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -490,6 +490,13 @@ def pretty_print_cycle(q) # :nodoc: end end +verbose, $VERBOSE = $VERBOSE, nil +begin + has_data_define = defined?(Data.define) +ensure + $VERBOSE = verbose +end + class Data # :nodoc: def pretty_print(q) # :nodoc: class_name = PP.mcall(self, Kernel, :class).name @@ -522,7 +529,7 @@ def pretty_print(q) # :nodoc: def pretty_print_cycle(q) # :nodoc: q.text sprintf("#", PP.mcall(self, Kernel, :class).name) end -end if defined?(Data.define) +end if has_data_define class Range # :nodoc: def pretty_print(q) # :nodoc: diff --git a/test/test_pp.rb b/test/test_pp.rb index e721260..28da00e 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -172,7 +172,14 @@ def test_struct assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup)) unless RUBY_ENGINE == "truffleruby" end - if defined?(Data.define) + verbose, $VERBOSE = $VERBOSE, nil + begin + has_data_define = defined?(Data.define) + ensure + $VERBOSE = verbose + end + + if has_data_define D = Data.define(:aaa, :bbb) def test_data a = D.new("aaa", "bbb")