-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding
Utils.inspect
to have Hash#inspect
looking same across
all Ruby versions.
- Loading branch information
Showing
3 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |