graph TD
N2[5]
N1[ARRAY] --> |0| N2
N3["foo"]
N1[ARRAY] --> |1| N3
N4[:a]
N1[ARRAY] --> |2| N4
N0[Foo] --> |"@a"| N1
N0[Foo] --> |"@b"| N0
N5[1]
N0[Foo] --> |"@c"| N5
N7[12]
N6[Hash] --> |0| N7
N8["aaa"]
N6[Hash] --> |a| N8
N9[:b]
N6[Hash] --> |key| N9
N0[Foo] --> |"@d"| N6
Last active
February 17, 2022 11:39
-
-
Save miura1729/dd29140b6fb595d13a6ae6ca7e86d39f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module GP | |
def gp(obj) | |
print "```mermaid\n" | |
print " graph TD\n" | |
gp_aux(obj, {}) | |
print "```" | |
end | |
def gp_aux(obj, occur) | |
label = occur[obj.__id__] | |
unless label | |
label = occur[obj.__id__] = "N#{occur.size}" | |
case obj | |
when String | |
print " #{label}[\"#{obj}\"]\n" | |
when Numeric, Symbol | |
print " #{label}[#{obj.inspect}]\n" | |
when Array | |
obj.each_with_index do |ivname, i| | |
val = obj[i] | |
vlabel = gp_aux(val, occur) | |
print " #{label}[ARRAY] --> |#{i}| #{vlabel}\n" | |
end | |
when Hash | |
obj.each do |key, val| | |
vlabel = gp_aux(val, occur) | |
print " #{label}[Hash] --> |#{key}| #{vlabel}\n" | |
end | |
else | |
#user defined object | |
obj.instance_variables.each do |ivname| | |
ivvalue = obj.instance_variable_get(ivname) | |
ivlabel = gp_aux(ivvalue, occur) | |
print " #{label}[#{obj.class.to_s}] --> |\"#{ivname}\"| #{ivlabel}\n" | |
end | |
end | |
end | |
label | |
end | |
end | |
if __FILE__ == $0 then | |
include GP | |
class Foo | |
def initialize | |
@a = [] | |
@a[0] = 5 | |
@a[1] = "foo" | |
@a[2] = :a | |
@b = self | |
@c = 1 | |
@d = {} | |
@d[0] = 12 | |
@d[:a] = "aaa" | |
@d["key"] = :b | |
end | |
end | |
gp(Foo.new) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment