Skip to content

Commit 165b8f5

Browse files
committed
adding Utils.inspect to have Hash#inspect looking same across
all Ruby versions.
1 parent 2979940 commit 165b8f5

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

lib/trailblazer/core.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "forwardable"
22
require "trailblazer/core/utils/convert_operation_test"
33
require "trailblazer/core/utils/symbol_inspect_for"
4+
require "trailblazer/core/utils/inspect"
45

56
module Trailblazer
67
module Core

lib/trailblazer/core/utils/inspect.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module Trailblazer
2+
module Core
3+
module Utils
4+
def self.inspect(object)
5+
return object.inspect unless object.is_a?(Hash)
6+
7+
old_string = object.inspect
8+
# old_string = %({{symbol: 1, "string" => 2},"string" => 1})
9+
10+
new_string = old_string.gsub(/(\w+): /, ':\1=>')
11+
new_string = new_string.gsub(" => ", "=>")
12+
13+
return new_string
14+
"asdfafasdff"
15+
16+
# if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.7.0") || RUBY_ENGINE == 'jruby'
17+
# "#{name}"
18+
# else
19+
# ":#{name}"
20+
# end
21+
end
22+
end
23+
end
24+
end

test/hash_inspect_test.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require "test_helper"
2+
3+
class HashInspectTest < Minitest::Spec
4+
class Memo < Struct.new(:id); end
5+
6+
it "converts Ruby 3.4+ {Hash#inspect} to old style, so our tests don't have to be changed" do
7+
hsh = {
8+
symbol: {symbol: 1, "string" => 2},
9+
"string" => 1,
10+
Memo.new(1) => true,
11+
}
12+
13+
puts Trailblazer::Core::Utils.inspect(hsh)
14+
assert_equal Trailblazer::Core::Utils.inspect(hsh), %({:symbol=>{:symbol=>1, "string"=>2}, "string"=>1, #<struct HashInspectTest::Memo id=1>=>true})
15+
end
16+
17+
it "uses native {#inspect} for other classes" do
18+
assert_equal Trailblazer::Core::Utils.inspect(Struct.new(:id).new(1)), %(#<struct id=1>)
19+
end
20+
end

0 commit comments

Comments
 (0)