Skip to content

Commit

Permalink
adding Utils.inspect to have Hash#inspect looking same across
Browse files Browse the repository at this point in the history
all Ruby versions.
  • Loading branch information
apotonick committed Nov 7, 2024
1 parent 2979940 commit 165b8f5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/trailblazer/core.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "forwardable"
require "trailblazer/core/utils/convert_operation_test"
require "trailblazer/core/utils/symbol_inspect_for"
require "trailblazer/core/utils/inspect"

module Trailblazer
module Core
Expand Down
24 changes: 24 additions & 0 deletions lib/trailblazer/core/utils/inspect.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Trailblazer
module Core
module Utils
def self.inspect(object)
return object.inspect unless object.is_a?(Hash)

old_string = object.inspect
# old_string = %({{symbol: 1, "string" => 2},"string" => 1})

new_string = old_string.gsub(/(\w+): /, ':\1=>')
new_string = new_string.gsub(" => ", "=>")

return new_string
"asdfafasdff"

# if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.7.0") || RUBY_ENGINE == 'jruby'
# "#{name}"
# else
# ":#{name}"
# end
end
end
end
end
20 changes: 20 additions & 0 deletions test/hash_inspect_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "test_helper"

class HashInspectTest < Minitest::Spec
class Memo < Struct.new(:id); end

it "converts Ruby 3.4+ {Hash#inspect} to old style, so our tests don't have to be changed" do
hsh = {
symbol: {symbol: 1, "string" => 2},
"string" => 1,
Memo.new(1) => true,
}

puts Trailblazer::Core::Utils.inspect(hsh)
assert_equal Trailblazer::Core::Utils.inspect(hsh), %({:symbol=>{:symbol=>1, "string"=>2}, "string"=>1, #<struct HashInspectTest::Memo id=1>=>true})
end

it "uses native {#inspect} for other classes" do
assert_equal Trailblazer::Core::Utils.inspect(Struct.new(:id).new(1)), %(#<struct id=1>)
end
end

0 comments on commit 165b8f5

Please sign in to comment.