Skip to content

Commit

Permalink
in #inspect, allow a string that can contain a hash.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Nov 12, 2024
1 parent 8382c61 commit d74b975
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
19 changes: 8 additions & 11 deletions lib/trailblazer/core/utils/inspect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@ module Trailblazer
module Core
module Utils
def self.inspect(object)
return Inspect.convert_hash_inspect(object) if object.is_a?(String)
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:\2=>')
new_string = new_string.gsub(" => ", "=>")

return new_string
"asdfafasdff"
Inspect.convert_hash_inspect(old_string)
end

# if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.7.0") || RUBY_ENGINE == 'jruby'
# "#{name}"
# else
# ":#{name}"
# end
module Inspect
def self.convert_hash_inspect(hash_inspect)
new_string = hash_inspect.gsub(/([{ ])(\w+): /, '\1:\2=>')
new_string.gsub(" => ", "=>")
end
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion test/hash_inspect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ class Memo < Struct.new(:id); end

#<Method: Trailb

puts Trailblazer::Core::Utils.inspect(hsh)
# 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, #<Method: HashInspectTest(Kernel)#inspect()>=>9})
end

it "can convert only the hash part of a longer string" do
assert_equal Trailblazer::Core::Utils.inspect("A string {with: true, and: 1} a hash"), %(A string {:with=>true, :and=>1} a hash)
end

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

0 comments on commit d74b975

Please sign in to comment.