Share
Embed
railsdm2019.pdf
Shuichi Tamayose
March 22, 2019
Transcript
149 slides
-
RAILS DEVELOPERS MEETUP 2019 SESSION: C - 10 2019/03/22 (ۚ)
ۄد मҰ Ruby ͷίʔυղੳͷʮ੩ʯͱʮಈʯ -
ࣈ۟ղੳ 1 require "ripper" 2 3 pp Ripper.lex("puts 2 +
2") Ruby ͷඪ४ϥΠϒϥϦʹ͋Δ Ripper.lex Λ͏͜ͱͰτʔΫϯใΛऔಘͰ͖Δ ਤ2. τʔΫϯྻΛಘΔͨΊͷίʔυ ߏจղੳͷલॲཧɺRuby ͷιʔεΛಡΈࠐΜͰτʔΫϯʹม͢Δૢ࡞ -
ࣈ۟ղੳ [[[1, 0], :on_ident, "puts", EXPR_CMDARG], [[1, 4], :on_sp, "
", EXPR_CMDARG], [[1, 5], :on_int, "2", EXPR_END], [[1, 6], :on_sp, " ", EXPR_END], [[1, 7], :on_op, "+", EXPR_BEG], [[1, 8], :on_sp, " ", EXPR_BEG], [[1, 9], :on_int, "2", EXPR_END], [[1, 10], :on_nl, "\n", EXPR_BEG]] 1 require "ripper" 2 3 pp Ripper.lex("puts 2 + 2") ʮҐஔใʯʮτʔΫϯͷछผʯʮτʔΫϯͷจࣈྻʯʮࣈ۟ղੳͷঢ়ଶʯ͕ಘΒΕΔ ਤ3. ಘΒΕͨτʔΫϯใ -
ࣈ۟ղੳ [[[1, 0], :on_ident, "puts", EXPR_CMDARG], [[1, 4], :on_sp, "
", EXPR_CMDARG], [[1, 5], :on_int, "2", EXPR_END], [[1, 6], :on_sp, " ", EXPR_END], [[1, 7], :on_op, "+", EXPR_BEG], [[1, 8], :on_sp, " ", EXPR_BEG], [[1, 9], :on_int, "2", EXPR_END], [[1, 10], :on_nl, "\n", EXPR_BEG]] 1 require "ripper" 2 3 pp Ripper.lex("puts 2 + 2") ʮҐஔใʯʮτʔΫϯͷछผʯʮτʔΫϯͷจࣈྻʯʮࣈ۟ղੳͷঢ়ଶʯ͕ಘΒΕΔ ਤ3. ಘΒΕͨτʔΫϯใ -
ࣈ۟ղੳ [[[1, 0], :on_ident, "puts", EXPR_CMDARG], [[1, 4], :on_sp, "
", EXPR_CMDARG], [[1, 5], :on_int, "2", EXPR_END], [[1, 6], :on_sp, " ", EXPR_END], [[1, 7], :on_op, "+", EXPR_BEG], [[1, 8], :on_sp, " ", EXPR_BEG], [[1, 9], :on_int, "2", EXPR_END], [[1, 10], :on_nl, "\n", EXPR_BEG]] ʮҐஔใʯʮτʔΫϯͷछผʯʮτʔΫϯͷจࣈྻʯʮࣈ۟ղੳͷঢ়ଶʯ͕ಘΒΕΔ ਤ3. ಘΒΕͨτʔΫϯใ 1 require "ripper" 2 3 pp Ripper.lex("puts 2 + 2") -
ࣈ۟ղੳ [[[1, 0], :on_ident, "puts", EXPR_CMDARG], [[1, 4], :on_sp, "
", EXPR_CMDARG], [[1, 5], :on_int, "2", EXPR_END], [[1, 6], :on_sp, " ", EXPR_END], [[1, 7], :on_op, "+", EXPR_BEG], [[1, 8], :on_sp, " ", EXPR_BEG], [[1, 9], :on_int, "2", EXPR_END], [[1, 10], :on_nl, "\n", EXPR_BEG]] ʮҐஔใʯʮτʔΫϯͷछผʯʮτʔΫϯͷจࣈྻʯʮࣈ۟ղੳͷঢ়ଶʯ͕ಘΒΕΔ ਤ3. ಘΒΕͨτʔΫϯใ 1 require "ripper" 2 3 pp Ripper.lex("puts 2 + 2") -
ࣈ۟ղੳ [[[1, 0], :on_ident, "puts", EXPR_CMDARG], [[1, 4], :on_sp, "
", EXPR_CMDARG], [[1, 5], :on_int, "2", EXPR_END], [[1, 6], :on_sp, " ", EXPR_END], [[1, 7], :on_op, "+", EXPR_BEG], [[1, 8], :on_sp, " ", EXPR_BEG], [[1, 9], :on_int, "2", EXPR_END], [[1, 10], :on_nl, "\n", EXPR_BEG]] ʮҐஔใʯʮτʔΫϯͷछผʯʮτʔΫϯͷจࣈྻʯʮࣈ۟ղੳͷঢ়ଶʯ͕ಘΒΕΔ ਤ3. ಘΒΕͨτʔΫϯใ 1 require "ripper" 2 3 pp Ripper.lex("puts 2 + 2") -
ࣈ۟ղੳ τʔΫϯͷछྨ༷ʑ :CHAR :__end__ :backref :backtick :comma :comment :const :cvar
:embdoc :embdoc_beg :embdoc_end :embexpr_beg :embexpr_end :embvar :float :gvar :heredoc_beg :heredoc_end :ident :ignored_nl :imaginary :int :ivar :kw :label :label_end :lbrace :lbracket :lparen :nl :op :period :qsymbols_beg :qwords_beg :rational :rbrace :rbracket :regexp_beg :regexp_end :rparen :semicolon :sp :symbeg :symbols_beg :tlambda :tlambeg :tstring_beg :tstring_content :tstring_end :words_beg :words_sep ਤ4. Ripper::SCANNER_EVENTS ͰಘΒΕΔτʔΫϯͷछྨ -
ࣈ۟ղੳ τʔΫϯྻͷঢ়ଶͰಘΒΕΔใ͕গͳ͍ͷͰ࣮ࡍͷղੳͰ͜ͷޙʹ ग़ͯ͘Δߏจ͕༻͞ΕΔ͜ͱ͕ଟ͍ [[[1, 0], :on_ident, "puts", EXPR_CMDARG], [[1, 4],
:on_sp, " ", EXPR_CMDARG], [[1, 5], :on_int, "2", EXPR_END], [[1, 6], :on_sp, " ", EXPR_END], [[1, 7], :on_op, "+", EXPR_BEG], [[1, 8], :on_sp, " ", EXPR_BEG], [[1, 9], :on_int, "2", EXPR_END], [[1, 10], :on_nl, "\n", EXPR_BEG]] -
ߏจղੳ (SCOPE@1:0-1:10 tbl: [] args: nil body: (FCALL@1:0-1:10 :puts (ARRAY@1:5-1:10
(OPCALL@1:5-1:10 (LIT@1:5-1:6 2) :+ (ARRAY@1:9-1:10 (LIT@1:9-1:10 2) nil)) nil))) ߏจΛಘΔͨΊͷίʔυ pp RubyVM::AbstractSyntaxTree.parse("puts 2 + 2") -
(SCOPE@1:0-1:10 tbl: [] args: nil body: (FCALL@1:0-1:10 :puts (ARRAY@1:5-1:10 (OPCALL@1:5-1:10
(LIT@1:5-1:6 2) :+ (ARRAY@1:9-1:10 (LIT@1:9-1:10 2) nil)) nil))) ͜ͷ··ͩͱগ͠ݟͮΒ͍ͷͰ؆қతʹදࣔ SCOPE table: [ none ] args: [ none ] FCALL method_id: puts OPCALL :+ LIT 2 LIT 2 -
[[[1, 0], :on_ident, "puts", EXPR_CMDARG], [[1, 4], :on_sp, " ",
EXPR_CMDARG], [[1, 5], :on_int, "2", EXPR_END], [[1, 6], :on_sp, " ", EXPR_END], [[1, 7], :on_op, "+", EXPR_BEG], [[1, 8], :on_sp, " ", EXPR_BEG], [[1, 9], :on_int, "2", EXPR_END], [[1, 10], :on_nl, "\n", EXPR_BEG]] SCOPE table: [ none ] args: [ none ] FCALL method_id: puts OPCALL :+ LIT 2 LIT 2 -
SCOPE table: [ none ] args: [ none ] FCALL
method_id: puts OPCALL :+ LIT 2 LIT 2 puts ͕ϝιου໊ͱ͍͏͜ͱ͕Θ͔Δ [[[1, 0], :on_ident, "puts", EXPR_CMDARG], [[1, 4], :on_sp, " ", EXPR_CMDARG], [[1, 5], :on_int, "2", EXPR_END], [[1, 6], :on_sp, " ", EXPR_END], [[1, 7], :on_op, "+", EXPR_BEG], [[1, 8], :on_sp, " ", EXPR_BEG], [[1, 9], :on_int, "2", EXPR_END], [[1, 10], :on_nl, "\n", EXPR_BEG]] -
SCOPE table: [ none ] args: [ none ] FCALL
method_id: puts OPCALL :+ LIT 2 LIT 2 puts ͷҾͱͯ͠ 2 + 2 ͕ݺͼग़͞Ε͍ͯΔ [[[1, 0], :on_ident, "puts", EXPR_CMDARG], [[1, 4], :on_sp, " ", EXPR_CMDARG], [[1, 5], :on_int, "2", EXPR_END], [[1, 6], :on_sp, " ", EXPR_END], [[1, 7], :on_op, "+", EXPR_BEG], [[1, 8], :on_sp, " ", EXPR_BEG], [[1, 9], :on_int, "2", EXPR_END], [[1, 10], :on_nl, "\n", EXPR_BEG]] -
ߏจղੳ RubyVM::AbstractSyntaxTree::Node#type ͰಘΒΕΔϊʔυͷछྨ (ࣗௐ) :SCOPE :BLOCK :IF :UNLESS :CASE :CASE2
:WHEN :WHILE :UNTIL :ITER :FOR :FOR_MASGN :BREAK :NEXT :REDO :RETRY :BEGIN :RESCUE :RESBODY :ENSURE :AND :OR :MASGN :LASGN :DASGN :DASGN_CURR :GASGN :IASGN :CDECL :CVASGN :OP_ASGN1 :OP_ASGN2 :OP_ASGN_AND :OP_ASGN_OR :OP_CDECL :CALL :OPCALL :FCALL :VCALL :QCALL :SUPER :ZSUPER :ARRAY :ZARRAY :VALUES :HASH :RETURN :YIELD :LVAR :DVAR :GVAR :IVAR :CONST :CVAR :NTH_REF :BACK_REF :MATCH :MATCH2 :MATCH3 :LIT :STR :DSTR :XSTR :DXSTR :EVSTR :DREGX :ONCE :ARGS :OPT_ARG :KW_ARG :POSTARG :METHREF :ARGSCAT :ARGSPUSH :SPLAT :BLOCK_PASS :DEFN :DEFS :ALIAS :VALIAS :UNDEF :CLASS :MODULE :SCLASS :COLON2 :COLON3 :DOT2 :DOT3 :FLIP2 :FLIP3 :SELF :NIL :TRUE :FALSE :ERRINFO :DEFINED :POSTEXE :DSYM :ATTRASGN :LAMBDA -
ߏؒͷฤूڑ (Tree Edit Distance) ɾ2 ͭͷॱং͖ߏ͕Ͳͷఔҟͳ͍ͬͯΔ͔Λࣔ͢ ɾʹର͢Δฤूૢ࡞࣍ͷ 3ͭ ɾϊʔυͷՃ ɾϊʔυͷআ
ɾϊʔυͷஔ ɾ2 ͭͷΛҰகͤ͞Δͷʹඞཁͳૢ࡞ͷճ͕ฤूڑ -
ฤूૢ࡞ (ϊʔυͷஔ) OPCALL :+ LIT 1 LIT 1 1 +
1 Λද͢ߏจ OPCALL :+ STR "foo" STR "bar" "foo" + "bar" Λද͢ߏจ ࠨͷߏ͔Βӈͷߏʹมܗ͍ͨ͠ ɾҰ൪࠷ͷखॱ LIT ϊʔυΛ STR ϊʔυʹஔ͖͑Δ͜ͱ -
ฤूૢ࡞ (ϊʔυͷஔ) OPCALL :+ STR "foo" STR "bar" OPCALL :+
STR "foo" STR "bar" ɾҰ൪࠷ͷखॱ LIT ϊʔυΛ STR ϊʔυʹஔ͖͑Δ͜ͱ LIT ϊʔυΛ STR ϊʔυʹஔ͢Δ ࠨͷߏ͔Βӈͷߏʹมܗ͍ͨ͠ -
ฤूૢ࡞ (ϊʔυͷஔ) OPCALL :+ STR "foo" STR "bar" OPCALL :+
STR "foo" STR "bar" ஔૢ࡞ʹΑͬͯ 2ͭͷϊʔυ͕Ұக͢Δ ɾࠓճͷ࣮Ͱϊʔυͷछผ͚ͩΛஔͷରͱͨ͠ -
ฤूૢ࡞ (ϊʔυͷՃ) OPCALL :+ LIT 1 LIT 1 ϊʔυΛҙͷՕॴʹՃ͢Δ Ճ
FCALL method_id: puts OPCALL :+ LIT 1 LIT 1 -
ฤूૢ࡞ (ϊʔυͷআ) ҙͷϊʔυΛআ͢Δ আ FCALL method_id: puts OPCALL :+ LIT
1 LIT 1 OPCALL :+ LIT 1 LIT 1 -
ast_distance gem 2 ͭͷ RubyVM::AbstractSyntaxTree::Node ͷฤूڑΛܭࢉ͢Δ gem ɾhttps://github.com/siman-man/ast_distance 1 require
"ast_distance" 2 3 ast1 = RubyVM::AbstractSyntaxTree.parse("1 + 1") 4 ast2 = RubyVM::AbstractSyntaxTree.parse("'hello' + 'world'") 5 6 puts ASTDistance.tree_edit_distance(ast1, ast2) #=> 2 ɾฤूڑͷ͕খ͍͞΄ͲࣅͨΑ͏ͳίʔυʹͳ͍ͬͯΔՄೳੑ͕ߴ͍ -
ast_distance gem 2 ͭͷ RubyVM::AbstractSyntaxTree::Node ͷฤूڑΛܭࢉ͢Δ gem ɾhttps://github.com/siman-man/ast_distance 1 require
"ast_distance" 2 3 ast1 = RubyVM::AbstractSyntaxTree.parse("1 + 1") 4 ast2 = RubyVM::AbstractSyntaxTree.parse("2 + 2") 5 6 puts ASTDistance.tree_edit_distance(ast1, ast2) #=> 0 ɾฤूڑͷ͕খ͍͞΄ͲࣅͨΑ͏ͳίʔυʹͳ͍ͬͯΔՄೳੑ͕ߴ͍ ฤूڑ͕ 0 ͩͱίʔυ͕͔ͳΓྨࣅ͍ͯ͠Δ -
flay gem ߏจʹରͯ͠ϋογϡΛܭࢉۙ͠ࣅ͍ͯ͠ΔίʔυΛݕग़͢Δ ɾhttps://github.com/seattlerb/flay ɾͪ͜ΒͰߏจղੳʹ ruby_parser ͕༻͞Ε͍ͯΔ 1 def add(a,
b) 2 a + b 3 end 4 5 def sub(a, b) 6 a - b 7 end $ flay -v --diff --mass=5 example.rb Processing example.rb Total score (lower is better) = 10 1) Similar code found in :defn (mass = 10) A: example.rb:1 B: example.rb:5 A: def add(a, b) B: def sub(a, b) A: (a + b) B: (a - b) end example.rb -
Ruby ιʔεϑΝΠϧؒͷґଘؔͷ୳ࡧ Ruby ͷϑΝΠϧؒͷґଘؔΛௐΔॲཧΛॻ͍ͯΈΔ 1 require "prime" 2 3 pp
2.prime? ɾྫ͑ԼͷྫͰ example.rb prime.rb ʹґଘ͍ͯ͠ΔͱΈͳ͢ example.rb ґଘؔͷఆʹ require load ͷଞͷ Ruby ίʔυͷ ಡΈࠐΈॲཧΛར༻͢Δ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ํ ಡΈࠐΈॲཧΛݟ͚ͭΔͨΊʹ FCALL ϊʔυΛར༻͢Δ ɾFCALL ϊʔυϨγʔόΛࢦఆ͠ͳ͍ϝιουݺͼग़͠ <FCALL> ├───── :require (method
id) └───── <ARRAY> ├───── <STR> │ └───── "prime" (value) └───── nil (unknown) require "prime" -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def inspector(ast:, &block) 2 ast.children.each do |child| 3
next unless child.instance_of?(RubyVM::AbstractSyntaxTree::Node) 4 5 yield child 6 7 inspector(ast: child, &block) 8 end 9 end શͯͷ RubyVM::AbstractSyntaxTree::Node ʹରͯ͠ॲཧΛߦ͑ΔΑ͏ͳ ϝιουΛఆٛ͢Δ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def inspector(ast:, &block) 2 ast.children.each do |child| 3
next unless child.instance_of?(RubyVM::AbstractSyntaxTree::Node) 4 5 yield child 6 7 inspector(ast: child, &block) 8 end 9 end શͯͷ RubyVM::AbstractSyntaxTree::Node ʹରͯ͠ॲཧΛߦ͑ΔΑ͏ͳ ϝιουΛఆٛ͢Δ RubyVM::AbstractSyntaxTree::Node#children ϝιουͰࢠϊʔυใΛऔಘ ࢠϊʔυใྻͰฦͬͯ͘Δ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def inspector(ast:, &block) 2 ast.children.each do |child| 3
next unless child.instance_of?(RubyVM::AbstractSyntaxTree::Node) 4 5 yield child 6 7 inspector(ast: child, &block) 8 end 9 end શͯͷ RubyVM::AbstractSyntaxTree::Node ʹରͯ͠ॲཧΛߦ͑ΔΑ͏ͳ ϝιουΛఆٛ͢Δ Node ͷΠϯελϯεͰແ͍߹ॲཧΛεΩοϓ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ <FCALL> ├───── :require (method id) └───── <ARRAY> ├───── <STR>
│ └───── "prime" (value) └───── nil (unknown) ࢠϊʔυ͕ RubyVM::AbstractSyntaxTree::Node ͱݶΒͳ͍ FCALL ͷࢠཁૉ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ <FCALL> ├───── :require (method id) └───── <ARRAY> ├───── <STR>
│ └───── "prime" (value) └───── nil (unknown) FCALL ͷࢠཁૉ ࢠϊʔυ͕ RubyVM::AbstractSyntaxTree::Node ͱݶΒͳ͍ Symbol ΦϒδΣΫτ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ <FCALL> ├───── :require (method id) └───── <ARRAY> ├───── <STR>
│ └───── "prime" (value) └───── nil (unknown) FCALL ͷࢠཁૉ ࢠϊʔυ͕ RubyVM::AbstractSyntaxTree::Node ͱݶΒͳ͍ Node ΦϒδΣΫτ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def inspector(ast:, &block) 2 ast.children.each do |child| 3
next unless child.instance_of?(RubyVM::AbstractSyntaxTree::Node) 4 5 yield child 6 7 inspector(ast: child, &block) 8 end 9 end શͯͷ RubyVM::AbstractSyntaxTree::Node ʹରͯ͠ॲཧΛߦ͑ΔΑ͏ͳ ϝιουΛఆٛ͢Δ Node ʹର͢Δॲཧͷݺͼग़͠ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def inspector(ast:, &block) 2 ast.children.each do |child| 3
next unless child.instance_of?(RubyVM::AbstractSyntaxTree::Node) 4 5 yield child 6 7 inspector(ast: child, &block) 8 end 9 end શͯͷ RubyVM::AbstractSyntaxTree::Node ʹରͯ͠ॲཧΛߦ͑ΔΑ͏ͳ ϝιουΛఆٛ͢Δ શͯͷϊʔυΛ୳ࡧ͢ΔͨΊʹ࠶ؼతʹ ॲཧΛݺͼग़͢ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def inspector(ast:, &block) 2 ast.children.each do |child| 3
next unless child.instance_of?(RubyVM::AbstractSyntaxTree::Node) 4 5 yield child 6 7 inspector(ast: child, &block) 8 end 9 end શͯͷ RubyVM::AbstractSyntaxTree::Node ʹରͯ͠ॲཧΛߦ͑ΔΑ͏ͳ ϝιουΛఆٛ͢Δ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def get_feature(node:) 2 case node.children[0] 3 when :require,
:require_relative, :load 4 node.children[1].children[0].children[0] 5 when :autoload 6 node.children[1].children[1].children[0] 7 end 8 end ϊʔυͷใ͔ΒͲͷ Ruby ϑΝΠϧΛಡΈࠐ͏ͱ͍ͯ͠Δͷ͔Λऔಘ͢Δ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def get_feature(node:) 2 case node.children[0] 3 when :require,
:require_relative, :load 4 node.children[1].children[0].children[0] 5 when :autoload 6 node.children[1].children[1].children[0] 7 end 8 end ϊʔυͷใ͔ΒͲͷ Ruby ϑΝΠϧΛಡΈࠐ͏ͱ͍ͯ͠Δͷ͔Λऔಘ͢Δ ࢠϊʔυΛͨͲͬͯΛऔಘ͢Δ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ <FCALL> ├───── :require (method id) └───── <ARRAY> ├───── <STR>
│ └───── "prime" (value) └───── nil (unknown) node.children[1].children[0].children[0] ϊʔυͷใ͔ΒͲͷ Ruby ϑΝΠϧΛಡΈࠐ͏ͱ͍ͯ͠Δͷ͔Λऔಘ͢Δ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ <FCALL> ├───── :require (method id) └───── <ARRAY> ├───── <STR>
│ └───── "prime" (value) └───── nil (unknown) ϊʔυͷใ͔ΒͲͷ Ruby ϑΝΠϧΛಡΈࠐ͏ͱ͍ͯ͠Δͷ͔Λऔಘ͢Δ node.children[1].children[0].children[0] -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ ϊʔυͷใ͔ΒͲͷ Ruby ϑΝΠϧΛಡΈࠐ͏ͱ͍ͯ͠Δͷ͔Λऔಘ͢Δ node.children[1].children[0].children[0] <FCALL> ├───── :require (method id)
└───── <ARRAY> ├───── <STR> │ └───── "prime" (value) └───── nil (unknown) -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ ϊʔυͷใ͔ΒͲͷ Ruby ϑΝΠϧΛಡΈࠐ͏ͱ͍ͯ͠Δͷ͔Λऔಘ͢Δ <FCALL> ├───── :require (method id) └─────
<ARRAY> ├───── <STR> │ └───── "prime" (value) └───── nil (unknown) node.children[1].children[0].children[0] -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ <FCALL> ├───── :require (method id) └───── <ARRAY> ├───── <STR>
│ └───── "prime" (value) └───── nil (unknown) node.children[1].children[0].children[0] ϊʔυͷใ͔ΒͲͷ Ruby ϑΝΠϧΛಡΈࠐ͏ͱ͍ͯ͠Δͷ͔Λऔಘ͢Δ తͷʹ౸ୡ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def get_feature(node:) 2 case node.children[0] 3 when :require,
:require_relative, :load 4 node.children[1].children[0].children[0] 5 when :autoload 6 node.children[1].children[1].children[0] 7 end 8 end ϊʔυͷใ͔ΒͲͷ Ruby ϑΝΠϧΛಡΈࠐ͏ͱ͍ͯ͠Δͷ͔Λऔಘ͢Δ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 LOAD_METHODS = %i(require require_relative load autoload) 2 3
def search_load_features(ast:) 4 features = [] 5 6 inspector(ast: ast) do |node| 7 next if node.type != :FCALL 8 next unless LOAD_METHODS.include?(node.children[0]) 9 10 features << get_feature(node: node) 11 end 12 13 features 14 end AST ใ͔Βϩʔυ͍ͯ͠ΔϥΠϒϥϦͷҰཡΛऔಘ͢Δ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 LOAD_METHODS = %i(require require_relative load autoload) 2 3
def search_load_features(ast:) 4 features = [] 5 6 inspector(ast: ast) do |node| 7 next if node.type != :FCALL 8 next unless LOAD_METHODS.include?(node.children[0]) 9 10 features << get_feature(node: node) 11 end 12 13 features 14 end AST ใ͔Βϩʔυ͍ͯ͠ΔϥΠϒϥϦͷҰཡΛऔಘ͢Δ inspector Λ༻ͯ͠ϊʔυશମʹରͯ͠ॲཧΛߦ͏ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 LOAD_METHODS = %i(require require_relative load autoload) 2 3
def search_load_features(ast:) 4 features = [] 5 6 inspector(ast: ast) do |node| 7 next if node.type != :FCALL 8 next unless LOAD_METHODS.include?(node.children[0]) 9 10 features << get_feature(node: node) 11 end 12 13 features 14 end 2 ͭͷϑΟϧλϦϯάॲཧΛೖΕ͍ͯΔ ɾFCALL ϊʔυ͔Ͳ͏͔ͷఆ ɾϥΠϒϥϦΛϩʔυ͢Δϝιου͔Ͳ͏͔ͷఆ AST ใ͔Βϩʔυ͍ͯ͠ΔϥΠϒϥϦͷҰཡΛऔಘ͢Δ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 LOAD_METHODS = %i(require require_relative load autoload) 2 3
def search_load_features(ast:) 4 features = [] 5 6 inspector(ast: ast) do |node| 7 next if node.type != :FCALL 8 next unless LOAD_METHODS.include?(node.children[0]) 9 10 features << get_feature(node: node) 11 end 12 13 features 14 end ϥΠϒϥϦ໊Λऔಘͯ͠ϦετʹՃ AST ใ͔Βϩʔυ͍ͯ͠ΔϥΠϒϥϦͷҰཡΛऔಘ͢Δ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 LOAD_METHODS = %i(require require_relative load autoload) 2 3
def search_load_features(ast:) 4 features = [] 5 6 inspector(ast: ast) do |node| 7 next if node.type != :FCALL 8 next unless LOAD_METHODS.include?(node.children[0]) 9 10 features << get_feature(node: node) 11 end 12 13 features 14 end AST ใ͔Βϩʔυ͍ͯ͠ΔϥΠϒϥϦͷҰཡΛऔಘ͢Δ औಘͨ͠ϥΠϒϥϦ໊ͷҰཡΛฦ͢ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def find_path(feature) 2 RubyVM.resolve_feature_path(feature).last 3 rescue LoadError 4
nil 5 end ϥΠϒϥϦ໊͔ΒϑΝΠϧύεΛऔಘ͢Δ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def find_path(feature) 2 RubyVM.resolve_feature_path(feature).last 3 rescue LoadError 4
nil 5 end ϥΠϒϥϦ໊͔ΒϑΝΠϧύεΛऔಘ͢Δ ϥΠϒϥϦ໊͔ΒϑΝΠϧύεΛऔಘ -
RubyVM.resolve_feature_path ɾRuby 2.6 ͰՃ͞Εͨػೳ require load ϑΝΠϧύεऔಘ ISeq ɾrequire Ͱଞͷ
Ruby ϓϩάϥϜΛಡΈࠐΉࡍʹͲͷϑΝΠϧ͕ ಡΈࠐ·ΕΔͷ͔ΛௐΔ͜ͱ͕ग़དྷΔ ϑΝΠϧύεݕࡧ ίϯύΠϧ ਤ4. Ruby ͷଞͷϓϩάϥϜͷಡΈࠐΈॲཧ -
RubyVM.resolve_feature_path require load ϑΝΠϧύεऔಘ ISeq ϑΝΠϧύεݕࡧ ίϯύΠϧ ɾRuby 2.6 ͰՃ͞Εͨػೳ
ɾrequire Ͱଞͷ Ruby ϓϩάϥϜΛಡΈࠐΉࡍʹͲͷϑΝΠϧ͕ ಡΈࠐ·ΕΔͷ͔ΛௐΔ͜ͱ͕ग़དྷΔ ਤ4. Ruby ͷଞͷϓϩάϥϜͷಡΈࠐΈॲཧ resolve_feature_path ͜͜·ͰΛΔ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def check_file_dependencies(path:) 2 file_dependencies = Hash.new { |hash,
key| hash[key] = [] } 3 ast = RubyVM::AbstractSyntaxTree.parse_file(path) 4 features = search_load_features(ast: ast) 5 6 features.each do |feature| 7 feature_path = find_path(feature) || 8 find_path(File.expand_path(feature, File.dirname(path))) 9 10 next if feature_path.nil? 11 12 file_dependencies[path] << feature_path 13 file_dependencies.merge!(check_file_dependencies(path: feature_path)) 14 end 15 16 file_dependencies 17 end ϑΝΠϧͷґଘؔΛௐΔ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def check_file_dependencies(path:) 2 file_dependencies = Hash.new { |hash,
key| hash[key] = [] } 3 ast = RubyVM::AbstractSyntaxTree.parse_file(path) 4 features = search_load_features(ast: ast) 5 6 features.each do |feature| 7 feature_path = find_path(feature) || 8 find_path(File.expand_path(feature, File.dirname(path))) 9 10 next if feature_path.nil? 11 12 file_dependencies[path] << feature_path 13 file_dependencies.merge!(check_file_dependencies(path: feature_path)) 14 end 15 16 file_dependencies 17 end ϑΝΠϧͷґଘؔΛௐΔ parse_file ͰҾʹ AST Λऔಘ͍ͨ͠ϑΝΠϧύεΛࢦఆ͢Δ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def check_file_dependencies(path:) 2 file_dependencies = Hash.new { |hash,
key| hash[key] = [] } 3 ast = RubyVM::AbstractSyntaxTree.parse_file(path) 4 features = search_load_features(ast: ast) 5 6 features.each do |feature| 7 feature_path = find_path(feature) || 8 find_path(File.expand_path(feature, File.dirname(path))) 9 10 next if feature_path.nil? 11 12 file_dependencies[path] << feature_path 13 file_dependencies.merge!(check_file_dependencies(path: feature_path)) 14 end 15 16 file_dependencies 17 end ϑΝΠϧͷґଘؔΛௐΔ AST ใ͔ΒಡΈࠐΜͰ͍ΔϥΠϒϥϦͷҰཡΛऔಘͯ͘͠Δ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def check_file_dependencies(path:) 2 file_dependencies = Hash.new { |hash,
key| hash[key] = [] } 3 ast = RubyVM::AbstractSyntaxTree.parse_file(path) 4 features = search_load_features(ast: ast) 5 6 features.each do |feature| 7 feature_path = find_path(feature) || 8 find_path(File.expand_path(feature, File.dirname(path))) 9 10 next if feature_path.nil? 11 12 file_dependencies[path] << feature_path 13 file_dependencies.merge!(check_file_dependencies(path: feature_path)) 14 end 15 16 file_dependencies 17 end ϑΝΠϧͷґଘؔΛௐΔ ֤ϥΠϒϥϦຖʹॲཧΛߦ͏ -
1 def check_file_dependencies(path:) 2 file_dependencies = Hash.new { |hash, key|
hash[key] = [] } 3 ast = RubyVM::AbstractSyntaxTree.parse_file(path) 4 features = search_load_features(ast: ast) 5 6 features.each do |feature| 7 feature_path = find_path(feature) || 8 find_path(File.expand_path(feature, File.dirname(path))) 9 10 next if feature_path.nil? 11 12 file_dependencies[path] << feature_path 13 file_dependencies.merge!(check_file_dependencies(path: feature_path)) 14 end 15 16 file_dependencies 17 end ಡΈࠐΈॲཧͷ୳ࡧ࣮ ϑΝΠϧͷґଘؔΛௐΔ ࠷ॳϥΠϒϥϦ໊Ͱ୳ͯ͠ɺ͠ݟ͔ͭΒͳ͍߹૬ରύε ͕ࢦఆ͞ΕͨͱԾఆͯ͠ઈରύεʹม͔ͯ͠Β࠶୳ࡧ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def check_file_dependencies(path:) 2 file_dependencies = Hash.new { |hash,
key| hash[key] = [] } 3 ast = RubyVM::AbstractSyntaxTree.parse_file(path) 4 features = search_load_features(ast: ast) 5 6 features.each do |feature| 7 feature_path = find_path(feature) || 8 find_path(File.expand_path(feature, File.dirname(path))) 9 10 next if feature_path.nil? 11 12 file_dependencies[path] << feature_path 13 file_dependencies.merge!(check_file_dependencies(path: feature_path)) 14 end 15 16 file_dependencies 17 end ϑΝΠϧͷґଘؔΛௐΔ ϑΝΠϧύε͕ݟ͔ͭΒͳ͍߹ॲཧΛεΩοϓ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def check_file_dependencies(path:) 2 file_dependencies = Hash.new { |hash,
key| hash[key] = [] } 3 ast = RubyVM::AbstractSyntaxTree.parse_file(path) 4 features = search_load_features(ast: ast) 5 6 features.each do |feature| 7 feature_path = find_path(feature) || 8 find_path(File.expand_path(feature, File.dirname(path))) 9 10 next if feature_path.nil? 11 12 file_dependencies[path] << feature_path 13 file_dependencies.merge!(check_file_dependencies(path: feature_path)) 14 end 15 16 file_dependencies 17 end ϑΝΠϧͷґଘؔΛௐΔ ґଘؔͷՃΛߦ͏ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def check_file_dependencies(path:) 2 file_dependencies = Hash.new { |hash,
key| hash[key] = [] } 3 ast = RubyVM::AbstractSyntaxTree.parse_file(path) 4 features = search_load_features(ast: ast) 5 6 features.each do |feature| 7 feature_path = find_path(feature) || 8 find_path(File.expand_path(feature, File.dirname(path))) 9 10 next if feature_path.nil? 11 12 file_dependencies[path] << feature_path 13 file_dependencies.merge!(check_file_dependencies(path: feature_path)) 14 end 15 16 file_dependencies 17 end ϑΝΠϧͷґଘؔΛௐΔ ґଘϑΝΠϧͷґଘؔΛௐΔͨΊʹ࠶ؼతʹ୳ࡧΛߦ͏ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮ 1 def check_file_dependencies(path:) 2 file_dependencies = Hash.new { |hash,
key| hash[key] = [] } 3 ast = RubyVM::AbstractSyntaxTree.parse_file(path) 4 features = search_load_features(ast: ast) 5 6 features.each do |feature| 7 feature_path = find_path(feature) || 8 find_path(File.expand_path(feature, File.dirname(path))) 9 10 next if feature_path.nil? 11 12 file_dependencies[path] << feature_path 13 file_dependencies.merge!(check_file_dependencies(path: feature_path)) 14 end 15 16 file_dependencies 17 end ϑΝΠϧͷґଘؔΛௐΔ ࠷ޙʹ݁ՌΛฦͯ͠ऴΘΓ -
ಡΈࠐΈॲཧͷ୳ࡧ࣮·ͱΊ 1 def inspector(ast:, &block) 2 ast.children.each do |child| 3
next unless child.instance_of?(RubyVM::AbstractSyntaxTree::Node) 4 5 yield child 6 7 inspector(ast: child, &block) 8 end 9 end શͯͷϊʔυʹରͯ͠ॲཧΛߦ͑ΔΑ͏ͳॲཧΛॻ͘ 1 def inspector(ast:, &block) 2 ast.children.each do |child| 3 next unless child.instance_of?(RubyVM::AbstractSyntaxTree::Node) 4 5 yield child 6 7 inspector(ast: child, &block) 8 end 9 end 10 11 def get_feature(node:) 12 case node.children[0] 13 when :require, :require_relative, :load 14 node.children[1].children[0].children[0] 15 when :autoload 16 node.children[1].children[1].children[0] 17 end 18 end 19 20 LOAD_METHODS = %i(require require_relative load autoload) 21 22 def search_load_features(ast:) 23 features = [] 24 25 inspect(ast: ast) do |node| 26 next if node.type != :FCALL 27 next unless LOAD_METHODS.include?(node.children[0]) 28 29 features << get_feature(node: node) 30 end 31 32 features 33 end 34 35 def find_path(feature) 36 RubyVM.resolve_feature_path(feature).last 37 rescue LoadError 38 nil 39 end 40 41 def check_file_dependencies(path:) 42 file_dependencies = Hash.new { |hash, key| hash[key] = [] } 43 ast = RubyVM::AbstractSyntaxTree.parse_file(path) 44 features = search_load_features(ast: ast) 45 46 features.each do |feature| 47 feature_path = find_path(feature) || 48 find_path(File.expand_path(feature, File.dirname(path))) 49 50 next if feature_path.nil? 51 52 file_dependencies[path] << feature_path 53 file_dependencies.merge!(check_file_dependencies(path: feature_path)) 54 end 55 56 file_dependencies 57 end check_file_dependencies.rb -
ಡΈࠐΈॲཧͷ୳ࡧ࣮·ͱΊ 11 def get_feature(node:) 12 case node.children[0] 13 when :require,
:require_relative, :load 14 node.children[1].children[0].children[0] 15 when :autoload 16 node.children[1].children[1].children[0] 17 end 18 end ϊʔυใ͔ΒಡΈࠐΜͰ͍ΔϥΠϒϥϦ໊Λऔಘ 1 def inspector(ast:, &block) 2 ast.children.each do |child| 3 next unless child.instance_of?(RubyVM::AbstractSyntaxTree::Node) 4 5 yield child 6 7 inspector(ast: child, &block) 8 end 9 end 10 11 def get_feature(node:) 12 case node.children[0] 13 when :require, :require_relative, :load 14 node.children[1].children[0].children[0] 15 when :autoload 16 node.children[1].children[1].children[0] 17 end 18 end 19 20 LOAD_METHODS = %i(require require_relative load autoload) 21 22 def search_load_features(ast:) 23 features = [] 24 25 inspect(ast: ast) do |node| 26 next if node.type != :FCALL 27 next unless LOAD_METHODS.include?(node.children[0]) 28 29 features << get_feature(node: node) 30 end 31 32 features 33 end 34 35 def find_path(feature) 36 RubyVM.resolve_feature_path(feature).last 37 rescue LoadError 38 nil 39 end 40 41 def check_file_dependencies(path:) 42 file_dependencies = Hash.new { |hash, key| hash[key] = [] } 43 ast = RubyVM::AbstractSyntaxTree.parse_file(path) 44 features = search_load_features(ast: ast) 45 46 features.each do |feature| 47 feature_path = find_path(feature) || 48 find_path(File.expand_path(feature, File.dirname(path))) 49 50 next if feature_path.nil? 51 52 file_dependencies[path] << feature_path 53 file_dependencies.merge!(check_file_dependencies(path: feature_path)) 54 end 55 56 file_dependencies 57 end check_file_dependencies.rb -
ಡΈࠐΈॲཧͷ୳ࡧ࣮·ͱΊ 20 LOAD_METHODS = %i(require require_relative load autoload) 21 22
def search_load_features(ast:) 23 features = [] 24 25 inspect(ast: ast) do |node| 26 next if node.type != :FCALL 27 next unless LOAD_METHODS.include?(node.children[0]) 28 29 features << get_feature(node: node) 30 end 31 32 features 33 end AST ใ͔Βϩʔυ͍ͯ͠ΔϥΠϒϥϦͷҰཡΛऔಘ͢Δ 1 def inspector(ast:, &block) 2 ast.children.each do |child| 3 next unless child.instance_of?(RubyVM::AbstractSyntaxTree::Node) 4 5 yield child 6 7 inspector(ast: child, &block) 8 end 9 end 10 11 def get_feature(node:) 12 case node.children[0] 13 when :require, :require_relative, :load 14 node.children[1].children[0].children[0] 15 when :autoload 16 node.children[1].children[1].children[0] 17 end 18 end 19 20 LOAD_METHODS = %i(require require_relative load autoload) 21 22 def search_load_features(ast:) 23 features = [] 24 25 inspect(ast: ast) do |node| 26 next if node.type != :FCALL 27 next unless LOAD_METHODS.include?(node.children[0]) 28 29 features << get_feature(node: node) 30 end 31 32 features 33 end 34 35 def find_path(feature) 36 RubyVM.resolve_feature_path(feature).last 37 rescue LoadError 38 nil 39 end 40 41 def check_file_dependencies(path:) 42 file_dependencies = Hash.new { |hash, key| hash[key] = [] } 43 ast = RubyVM::AbstractSyntaxTree.parse_file(path) 44 features = search_load_features(ast: ast) 45 46 features.each do |feature| 47 feature_path = find_path(feature) || 48 find_path(File.expand_path(feature, File.dirname(path))) 49 50 next if feature_path.nil? 51 52 file_dependencies[path] << feature_path 53 file_dependencies.merge!(check_file_dependencies(path: feature_path)) 54 end 55 56 file_dependencies 57 end check_file_dependencies.rb -
ಡΈࠐΈॲཧͷ୳ࡧ࣮·ͱΊ 35 def find_path(feature) 36 RubyVM.resolve_feature_path(feature).last 37 rescue LoadError 38
nil 39 end ϥΠϒϥϦ໊͔ΒϑΝΠϧύεΛऔಘ͢Δ 1 def inspector(ast:, &block) 2 ast.children.each do |child| 3 next unless child.instance_of?(RubyVM::AbstractSyntaxTree::Node) 4 5 yield child 6 7 inspector(ast: child, &block) 8 end 9 end 10 11 def get_feature(node:) 12 case node.children[0] 13 when :require, :require_relative, :load 14 node.children[1].children[0].children[0] 15 when :autoload 16 node.children[1].children[1].children[0] 17 end 18 end 19 20 LOAD_METHODS = %i(require require_relative load autoload) 21 22 def search_load_features(ast:) 23 features = [] 24 25 inspect(ast: ast) do |node| 26 next if node.type != :FCALL 27 next unless LOAD_METHODS.include?(node.children[0]) 28 29 features << get_feature(node: node) 30 end 31 32 features 33 end 34 35 def find_path(feature) 36 RubyVM.resolve_feature_path(feature).last 37 rescue LoadError 38 nil 39 end 40 41 def check_file_dependencies(path:) 42 file_dependencies = Hash.new { |hash, key| hash[key] = [] } 43 ast = RubyVM::AbstractSyntaxTree.parse_file(path) 44 features = search_load_features(ast: ast) 45 46 features.each do |feature| 47 feature_path = find_path(feature) || 48 find_path(File.expand_path(feature, File.dirname(path))) 49 50 next if feature_path.nil? 51 52 file_dependencies[path] << feature_path 53 file_dependencies.merge!(check_file_dependencies(path: feature_path)) 54 end 55 56 file_dependencies 57 end check_file_dependencies.rb -
ಡΈࠐΈॲཧͷ୳ࡧ࣮·ͱΊ 41 def check_file_dependencies(path:) 42 file_dependencies = Hash.new { |hash,
key| hash[key] = [] } 43 ast = RubyVM::AbstractSyntaxTree.parse_file(path) 44 features = search_load_features(ast: ast) 45 46 features.each do |feature| 47 feature_path = find_path(feature) || 48 find_path(File.expand_path(feature, File.dirname(path))) 49 50 next if feature_path.nil? 51 52 file_dependencies[path] << feature_path 53 file_dependencies.merge!(check_file_dependencies(path: feature_path)) 54 end 55 56 file_dependencies 57 end ϑΝΠϧͷґଘؔΛௐΔ 1 def inspector(ast:, &block) 2 ast.children.each do |child| 3 next unless child.instance_of?(RubyVM::AbstractSyntaxTree::Node) 4 5 yield child 6 7 inspector(ast: child, &block) 8 end 9 end 10 11 def get_feature(node:) 12 case node.children[0] 13 when :require, :require_relative, :load 14 node.children[1].children[0].children[0] 15 when :autoload 16 node.children[1].children[1].children[0] 17 end 18 end 19 20 LOAD_METHODS = %i(require require_relative load autoload) 21 22 def search_load_features(ast:) 23 features = [] 24 25 inspect(ast: ast) do |node| 26 next if node.type != :FCALL 27 next unless LOAD_METHODS.include?(node.children[0]) 28 29 features << get_feature(node: node) 30 end 31 32 features 33 end 34 35 def find_path(feature) 36 RubyVM.resolve_feature_path(feature).last 37 rescue LoadError 38 nil 39 end 40 41 def check_file_dependencies(path:) 42 file_dependencies = Hash.new { |hash, key| hash[key] = [] } 43 ast = RubyVM::AbstractSyntaxTree.parse_file(path) 44 features = search_load_features(ast: ast) 45 46 features.each do |feature| 47 feature_path = find_path(feature) || 48 find_path(File.expand_path(feature, File.dirname(path))) 49 50 next if feature_path.nil? 51 52 file_dependencies[path] << feature_path 53 file_dependencies.merge!(check_file_dependencies(path: feature_path)) 54 end 55 56 file_dependencies 57 end check_file_dependencies.rb -
ϑΝΠϧؒͷґଘؔΛௐΔ ࣮ࡍʹಈ͔ͯ͠ΈΔ 1 _ext, fpath = RubyVM.resolve_feature_path('prime') 2 pp check_file_dependencies(path:
fpath) $ ruby check_file_dependencies.rb {"/Users/siman/.rbenv/versions/2.7.0-dev/lib/ruby/2.7.0/prime.rb"=> ["/Users/siman/.rbenv/versions/2.7.0-dev/lib/ruby/2.7.0/singleton.rb", "/Users/siman/.rbenv/versions/2.7.0-dev/lib/ruby/2.7.0/forwardable.rb"], "/Users/siman/.rbenv/versions/2.7.0-dev/lib/ruby/2.7.0/forwardable.rb"=> ["/Users/siman/.rbenv/versions/2.7.0-dev/lib/ruby/2.7.0/forwardable/impl.rb"]} -
ϑΝΠϧؒͷґଘؔΛௐΔ ͜ͷ··ͩͱΘ͔Γʹ͍͘ͷͰϑΝΠϧ໊͚ͩൈ͖ग़͢ {"/Users/siman/.rbenv/versions/2.7.0-dev/lib/ruby/2.7.0/prime.rb"=> ["/Users/siman/.rbenv/versions/2.7.0-dev/lib/ruby/2.7.0/singleton.rb", "/Users/siman/.rbenv/versions/2.7.0-dev/lib/ruby/2.7.0/forwardable.rb"], "/Users/siman/.rbenv/versions/2.7.0-dev/lib/ruby/2.7.0/forwardable.rb"=> ["/Users/siman/.rbenv/versions/2.7.0-dev/lib/ruby/2.7.0/forwardable/impl.rb"]} { "prime.rb"=> ["singleton.rb",
"forwardable.rb"], "forwardable.rb"=> ["impl.rb"] } -
ࣦഊ͢Δέʔε Ruby ॊೈͳॻ͖ํ͕ग़དྷΔͷͰɺશͯͷέʔεʹରԠ͢Δͷ͍͕͠ ॲཧʹࣦഊ͢Δύλʔϯྫ # มͰࢦఆ fname = 'set' require
fname # ͕ࣜ͞Ε͍ͯΔ require %w(s e t).join # eval eval('require "set"') ී௨ͷਓ͕ॻ͘Α͏ͳέʔε͚ͩରԠ͓͚ͯ͠େମΧόʔͰ͖Δ -
ࣦഊ͢Δέʔε ॲཧʹࣦഊ͢Δύλʔϯྫ # มͰࢦఆ fname = 'set' require fname #
͕ࣜ͞Ε͍ͯΔ require %w(p r i m e).join # eval eval('require "set"') มͰͷࢦఆՄೳੑ͕͋ΔͷͰରԠ͍ͨ͠ Ruby ॊೈͳॻ͖ํ͕ग़དྷΔͷͰɺશͯͷέʔεʹରԠ͢Δͷ͍͕͠ ී௨ͷਓ͕ॻ͘Α͏ͳέʔε͚ͩରԠ͓͚ͯ͠େମΧόʔͰ͖Δ -
ࣦഊ͢Δέʔε ॲཧʹࣦഊ͢Δύλʔϯྫ # มͰࢦఆ fname = 'prime' require fname #
͕ࣜ͞Ε͍ͯΔ require %w(s e t).join # eval eval('require "set"') ී௨ͷίʔυͰͳ͍ͷͰରԠͷ༏ઌ͍ Ruby ॊೈͳॻ͖ํ͕ग़དྷΔͷͰɺશͯͷέʔεʹରԠ͢Δͷ͍͕͠ ී௨ͷਓ͕ॻ͘Α͏ͳέʔε͚ͩରԠ͓͚ͯ͠େମΧόʔͰ͖Δ -
ISeq ISeqΛಘΔͨΊͷίʔυ RubyVM::InstructionSequence.compile_option = false iseq = RubyVM::InstructionSequence.compile("puts 2 +
2") pp iseq.to_a[13] ղੳΛ؆ܿʹ͢ΔͨΊʹίϯύΠϧΦϓγϣϯΛ͓ͬͯ͘ -
ISeq ISeqΛಘΔͨΊͷίʔυ RubyVM::InstructionSequence.compile_option = false iseq = RubyVM::InstructionSequence.compile("puts 2 +
2") pp iseq.to_a[13] [1, :RUBY_EVENT_LINE, [:putself], [:putobject, 2], [:putobject, 2], [:send, {:mid=>:+, :flag=>16, :orig_argc=>1}, false, nil], [:send, {:mid=>:puts, :flag=>20, :orig_argc=>1}, false, nil], [:leave]] -
ISeq [1, :RUBY_EVENT_LINE, [:putself], [:putobject, 2], [:putobject, 2], [:send, {:mid=>:+,
:flag=>16, :orig_argc=>1}, false, nil], [:send, {:mid=>:puts, :flag=>20, :orig_argc=>1}, false, nil], [:leave]] ಘΒΕͨ ISeq ΛγϛϡϨʔτ͢Δ͜ͱͰಈ࡞ͷղੳ͕ՄೳʹͳΔ ෦ελοΫ -
ISeq [1, :RUBY_EVENT_LINE, [:putself], [:putobject, 2], [:putobject, 2], [:send, {:mid=>:+,
:flag=>16, :orig_argc=>1}, false, nil], [:send, {:mid=>:puts, :flag=>20, :orig_argc=>1}, false, nil], [:leave]] ಘΒΕͨ ISeq ΛγϛϡϨʔτ͢Δ͜ͱͰಈ࡞ͷղੳ͕ՄೳʹͳΔ ෦ελοΫ self self ΛελοΫʹ push ͢Δ -
ISeq [1, :RUBY_EVENT_LINE, [:putself], [:putobject, 2], [:putobject, 2], [:send, {:mid=>:+,
:flag=>16, :orig_argc=>1}, false, nil], [:send, {:mid=>:puts, :flag=>20, :orig_argc=>1}, false, nil], [:leave]] ಘΒΕͨ ISeq ΛγϛϡϨʔτ͢Δ͜ͱͰಈ࡞ͷղੳ͕ՄೳʹͳΔ ෦ελοΫ self 2 2 ΛελοΫʹ push ͢Δ -
ISeq [1, :RUBY_EVENT_LINE, [:putself], [:putobject, 2], [:putobject, 2], [:send, {:mid=>:+,
:flag=>16, :orig_argc=>1}, false, nil], [:send, {:mid=>:puts, :flag=>20, :orig_argc=>1}, false, nil], [:leave]] ಘΒΕͨ ISeq ΛγϛϡϨʔτ͢Δ͜ͱͰಈ࡞ͷղੳ͕ՄೳʹͳΔ ෦ελοΫ self 2 2 2 ΛελοΫʹ push ͢Δ -
ISeq [1, :RUBY_EVENT_LINE, [:putself], [:putobject, 2], [:putobject, 2], [:send, {:mid=>:+,
:flag=>16, :orig_argc=>1}, false, nil], [:send, {:mid=>:puts, :flag=>20, :orig_argc=>1}, false, nil], [:leave]] ಘΒΕͨ ISeq ΛγϛϡϨʔτ͢Δ͜ͱͰಈ࡞ͷղੳ͕ՄೳʹͳΔ ෦ελοΫ self 4 + ϝιουͷݺͼग़͠ ϨγʔόʔͱҾ 1ͭΛελοΫ͔ΒऔΓग़ͯ͠ ϝιουͷฦΓΛελοΫʹ push ͢Δ -
ISeq [1, :RUBY_EVENT_LINE, [:putself], [:putobject, 2], [:putobject, 2], [:send, {:mid=>:+,
:flag=>16, :orig_argc=>1}, false, nil], [:send, {:mid=>:puts, :flag=>20, :orig_argc=>1}, false, nil], [:leave]] ಘΒΕͨ ISeq ΛγϛϡϨʔτ͢Δ͜ͱͰಈ࡞ͷղੳ͕ՄೳʹͳΔ ෦ελοΫ nil puts ϝιουͷݺͼग़͠ ϨγʔόʔͱҾ 1ͭΛελοΫ͔ΒऔΓग़ͯ͠ ϝιουͷฦΓΛελοΫʹ push ͢Δ -
ISeq [1, :RUBY_EVENT_LINE, [:putself], [:putobject, 2], [:putobject, 2], [:send, {:mid=>:+,
:flag=>16, :orig_argc=>1}, false, nil], [:send, {:mid=>:puts, :flag=>20, :orig_argc=>1}, false, nil], [:leave]] ಘΒΕͨ ISeq ΛγϛϡϨʔτ͢Δ͜ͱͰಈ࡞ͷղੳ͕ՄೳʹͳΔ ෦ελοΫ ࠓ͍Δείʔϓ͔Βൈ͚ग़͢ ελοΫͷҰ൪্ʹ͋ΔΛฦ͢ -
׆༻ྫ (ruby-type-profiler) https://github.com/mame/ruby-type-profiler ISeq ΛγϛϡϨʔτ֤ͯ͠ϝιουʹͲͷΑ͏ͳ͕͞Εͯ ͲͷΑ͏ͳΛฦ͍ͯ͠Δͷ͔ͱ͍͏ใΛू͢Δ ɾ෦ͰܕใΛू͢ΔͨΊʹಛԽͨ͠ RubyVM ͕࣮͞Ε͍ͯΔ 1
# test.rb 2 def foo(x) 3 if x > 10 4 x.to_s 5 else 6 x.boo() 7 x + 42 8 end 9 end 10 11 foo(42) $ exe/type-profiler test.rb test.rb:6: [error] undefined method: Integer#boo Object#foo :: (Integer) -> (Integer | String) -
੩తղੳ·ͱΊ ੩తղੳͰʮτʔΫϯྻʯʮߏจʯʮISeqʯͷ 3ͭͷσʔλ͔ΒղੳΛߦ͑Δ τʔΫϯྻ ߏจ ISeq ɾʮτʔΫϯྻʯಘΒΕΔใ͕ଞͷͷʹൺΔͱ͋͘͠·Γ׆༻͞Ε͍ͯͳ͍ ɾʮߏจʯಘΒΕΔใ͕ଟ͘ & औѻ͕༰қͳͨΊղੳͰΑ͘༻͞Ε͍ͯΔ
ɾʮISeqʯ VM ࣮͑ͯ͞͠͠·͑ΑΓৄࡉͳղੳ͕ՄೳͱͳΔ -
ࢀߟॻ੶ ʮRuby ͷ͘͠Έ Ruby Under a Microscopeʯ Pat Shaughnessy ஶ
ౡాߒೋɾ֯୩৴ଠ ڞ༁ Ruby ͷৄࡉΛΓ͍ͨ߹ͥͻ -
TracePoint Πϕϯτ໊ આ໌ :line ࣜͷධՁ :class ΫϥεఆٛɺಛҟΫϥεఆٛɺϞδϡʔϧఆٛͷಥೖ :end ΫϥεఆٛɺಛҟΫϥεఆٛɺϞδϡʔϧఆٛͷऴྃ :call
Ruby Ͱهड़͞Εͨϝιουͷݺͼग़͠ :return Ruby Ͱهड़͞Εͨϝιουݺͼग़͔͠ΒͷϦλʔϯ :c_call C Ͱهड़͞Εͨϝιουͷݺͼग़͠ :c_return C Ͱهड़͞Εͨϝιουݺͼग़͔͠ΒͷϦλʔϯ :raise ྫ֎ͷൃੜ :b_call ϒϩοΫͷ։࢝ :b_return ϒϩοΫͷऴྃ :thread_begin εϨουͷ։࢝ :thread_end εϨουͷऴྃ :fiber_switch ϑΝΠόʔͷΓସ͑ :script_compiled Ruby ίʔυ͕ ISeq ʹίϯύΠϧ͞Εͨ࣌ -
TracePoint Πϕϯτ໊ આ໌ :line ࣜͷධՁ :class ΫϥεఆٛɺಛҟΫϥεఆٛɺϞδϡʔϧఆٛͷಥೖ :end ΫϥεఆٛɺಛҟΫϥεఆٛɺϞδϡʔϧఆٛͷऴྃ :call
Ruby Ͱهड़͞Εͨϝιουͷݺͼग़͠ :return Ruby Ͱهड़͞Εͨϝιουݺͼग़͔͠ΒͷϦλʔϯ :c_call C Ͱهड़͞Εͨϝιουͷݺͼग़͠ :c_return C Ͱهड़͞Εͨϝιουݺͼग़͔͠ΒͷϦλʔϯ :raise ྫ֎ͷൃੜ :b_call ϒϩοΫͷ։࢝ :b_return ϒϩοΫͷऴྃ :thread_begin εϨουͷ։࢝ :thread_end εϨουͷऴྃ :fiber_switch ϑΝΠόʔͷΓସ͑ :script_compiled Ruby ίʔυ͕ ISeq ʹίϯύΠϧ͞Εͨ࣌ -
script_compiled Πϕϯτ ྫ͑ɺࠓճಋೖ͞Εͨ script_compiled ΠϕϯτΛ༻͍Δͱɺ࣮ࡍʹͲ͜Ͱιʔείʔυ ͕ϩʔυ͞Ε͔͕ͨΘ͔Γ·͢ɻಛʹɺ eval ͷར༻Λݕ͢Δ͜ͱ͕ग़དྷ·͢ɻେنϓ ϩδΣΫτͰɺͳ͔ͥྑ͘Θ͔Βͳ͍ڍಈΛ͢Δɺʮ͔ͨ͠͠ΒͲ͔ͬͰҙਤ͠ͳ͍ eval
͕͋Δ͔ʁʯͱ͍ͬͨͱ͖ʹɺͬͦ͜Γؚ·Ε͍ͯͨ eval Λݕग़͢ΔɺͳͲͱ͍ͬ ͨԠ༻͕Ͱ͖Δ͔͠Ε·ͤΜɻ Ҿ༻ݩ: Ruby 2.6 ͷվળΛࣗຫ͍ͨ͠, https://techlife.cookpad.com/entry/2018/12/27/093914 Ruby 2.6 Ͱ৽͘͠Ճ͞ΕͨΠϕϯτ Ruby ίʔυ͕ ISeq ʹίϯύΠϧ͞ΕͨλΠϛϯάͰϑοΫͰ͖Δ -
script_compiled Πϕϯτ eval Λݕ͢ΔͨΊͷ؆қతͳίʔυྫ eval_detector.rb 1 TracePoint.trace(:script_compiled) do |tp| 2
next if tp.method_id != :eval 3 4 puts "#{tp.path}:#{tp.lineno}: #{tp.eval_script.inspect}" 5 end 6 7 eval("1 + 1") -
1 TracePoint.trace(:script_compiled) do |tp| 2 next if tp.method_id != :eval
3 4 puts "#{tp.path}:#{tp.lineno}: #{tp.eval_script.inspect}" 5 end 6 7 eval("1 + 1") script_compiled Πϕϯτ eval_detector.rb script_compiled ΠϕϯτΛࢦఆ eval Λݕ͢ΔͨΊͷ؆қతͳίʔυྫ -
script_compiled Πϕϯτ eval Λݕ͢ΔͨΊͷ؆қతͳίʔυྫ eval_detector.rb 1 TracePoint.trace(:script_compiled) do |tp| 2
next if tp.method_id != :eval 3 4 puts "#{tp.path}:#{tp.lineno}: #{tp.eval_script.inspect}" 5 end 6 7 eval("1 + 1") eval ϝιουҎ֎ॲཧΛεΩοϓ -
1 TracePoint.trace(:script_compiled) do |tp| 2 next if tp.method_id != :eval
3 4 puts "#{tp.path}:#{tp.lineno}: #{tp.eval_script.inspect}" 5 end 6 7 eval("1 + 1") script_compiled Πϕϯτ eval_detector.rb TracePoint#eval_script Ͱ eval ʹ ͞ΕͨจࣈྻΛऔಘग़དྷΔ eval Λݕ͢ΔͨΊͷ؆қతͳίʔυྫ -
script_compiled Πϕϯτ eval_detector.rb:7: "1 + 1" ࣮ߦ݁Ռ eval_detector.rb 1 TracePoint.trace(:script_compiled)
do |tp| 2 next if tp.method_id != :eval 3 4 puts "#{tp.path}:#{tp.lineno}: #{tp.eval_script.inspect}" 5 end 6 7 eval("1 + 1") eval Λݕ͢ΔͨΊͷ؆қతͳίʔυྫ -
script_compiled Πϕϯτ ͳ͓ɺ͍ͳ͜ͱʹɺscript_compiled ΠϕϯτͰɺίϯύΠϧ݁Ռͷ ISeq Λࠩ͠ସ͑Δ ͜ͱग़དྷ·ͤΜɻฏอͨΕ·ͨ͠ɻ Ҿ༻ݩ: Ruby 2.6
ͷվળΛࣗຫ͍ͨ͠, https://techlife.cookpad.com/entry/2018/12/27/093914 -
script_compiled Πϕϯτ ͳ͓ɺ͍ͳ͜ͱʹɺscript_compiled ΠϕϯτͰɺίϯύΠϧ݁Ռͷ ISeq Λࠩ͠ସ͑Δ ͜ͱग़དྷ·ͤΜɻฏอͨΕ·ͨ͠ɻ Ҿ༻ݩ: Ruby 2.6
ͷվળΛࣗຫ͍ͨ͠, https://techlife.cookpad.com/entry/2018/12/27/093914 -
RubyVM::InstructionSequence.load_iseq 1 class RubyVM::InstructionSequence 2 def self.load_iseq(fpath) 3 iseq =
RubyVM::InstructionSequence.compile_file(fpath) 4 iseq 5 end 6 end iseq Λͦͷ··ฦ͢ฏͳίʔυ -
RubyVM::InstructionSequence.load_iseq 1 class RubyVM::InstructionSequence 2 def self.load_iseq(fpath) 3 iseq =
RubyVM::InstructionSequence.compile_file(fpath) . . . . . . iseq . end . end ແ๏ଳ iseq Λฦ͢·Ͱͷؒʹ৭ʑͳॲཧΛڬΉ͜ͱ͕Մೳ -
RubyVM::InstructionSequence.load_iseq ྫ͑ผͷ iseq ʹஔ͖͑ͯΈΔ 1 class RubyVM::InstructionSequence 2 def self.load_iseq(fname)
3 iseq = RubyVM::InstructionSequence.compile_file(fname) 4 5 _ext, fpath = RubyVM.resolve_feature_path('set') 6 iseq = RubyVM::InstructionSequence.compile_file(fpath) 7 8 iseq 9 end 10 end -
RubyVM::InstructionSequence.load_iseq ҙਤͨ͠ϥΠϒϥϦ͕औಘग़དྷͣʹΤϥʔʹͳΔ 1 require 'prime' 2 3 p 2.prime? #=>
undefined method `prime?' for 2:Integer (NoMethodError) 4 5 set = Set.new 6 set.add(1) 7 p set #=> #<Set: {1}> prime ͷΘΓʹ set ͕͑ΔΑ͏ʹͳ͍ͬͯΔ -
RubyVM::InstructionSequence.load_iseq ྫ͑ίϯύΠϧ݁ՌΛΩϟογϡͯ͠ΈΔ 1 BYTECODE_CACHE = Hash.new 2 3 class RubyVM::InstructionSequence
4 def self.load_iseq(fname) 5 return BYTECODE_CACHE[fname] if BYTECODE_CACHE[fname] 6 7 iseq = RubyVM::InstructionSequence.compile_file(fname) 8 BYTECODE_CACHE[fname] = iseq 9 end 10 end -
RubyVM::InstructionSequence.load_iseq Ωϟογϡͷ༗Γແ͠ͰͲͷఔ͕͕ࠩग़Δͷ͔ϕϯνϚʔΫΛऔͬͯΈΔ 1 require 'benchmark' 2 3 _ext, fpath =
RubyVM.resolve_feature_path('ripper') 4 5 Benchmark.bm do |r| 6 r.report 'load' do 7 100_000.times do 8 load fpath 9 end 10 end 11 end -
RubyVM::InstructionSequence.load_iseq ϕϯνϚʔΫ݁Ռ user system total real load 7.882627 4.416037 12.298664
( 12.338419) user system total real load 0.803963 0.931453 1.735416 ( 1.737521) Ωϟογϡແ͠ Ωϟογϡ༗Γ -
ϑΝΠϧؒͷґଘ୳ࡧ 2 ॲཧʹࣦഊ͢Δύλʔϯྫ # มͰࢦఆ fname = 'set' require fname
# ͕ࣜ͞Ε͍ͯΔ require %w(s e t).join # eval eval('require "set"') TracePoint Λར༻ͯ͠੩తղੳͰࠔͩͬͨ෦Λղܾ͍ͯ͘͠ ɾrequire ʹ͞ΕΔ͕Θ͔Βͳ͍ -
ϑΝΠϧؒͷґଘ୳ࡧ 2 1 TracePoint.trace(:call) do |tp| 2 next if tp.method_id
!= :require 3 4 bl = caller_locations[1] 5 feature = tp.binding.local_variable_get(tp.parameters.first.last) 6 7 puts "#{bl.path} --> #{feature}" 8 end ґଘϑΝΠϧ୳ࡧεΫϦϓτ ( TracePoint ൛) -
ϑΝΠϧؒͷґଘ୳ࡧ 2 1 TracePoint.trace(:call) do |tp| 2 next if tp.method_id
!= :require 3 4 bl = caller_locations[1] 5 feature = tp.binding.local_variable_get(tp.parameters.first.last) 6 7 puts "#{bl.path} --> #{feature}" 8 end ґଘϑΝΠϧ୳ࡧεΫϦϓτ ( TracePoint ൛) call ΠϕϯτΛࢦఆ (Ruby ͷϝιουͷݺͼग़͠) require ʹؔͯ͠ rubygems Ͱ࠶ఆٛ͞Ε͍ͯΔͷͰ call ΠϕϯτͰั·͑Δ͜ͱ͕ग़དྷΔ -
ϑΝΠϧؒͷґଘ୳ࡧ 2 1 TracePoint.trace(:call) do |tp| 2 next if tp.method_id
!= :require 3 4 bl = caller_locations[1] 5 feature = tp.binding.local_variable_get(tp.parameters.first.last) 6 7 puts "#{bl.path} --> #{feature}" 8 end ґଘϑΝΠϧ୳ࡧεΫϦϓτ ( TracePoint ൛) TracePoint#method_id Ͱݺͼग़͞Εͨϝιου໊Λऔಘ require Ҏ֎ॲཧΛεΩοϓ͢Δ -
ϑΝΠϧؒͷґଘ୳ࡧ 2 1 TracePoint.trace(:call) do |tp| 2 next if tp.method_id
!= :require 3 4 bl = caller_locations[1] 5 feature = tp.binding.local_variable_get(tp.parameters.first.last) 6 7 puts "#{bl.path} --> #{feature}" 8 end ґଘϑΝΠϧ୳ࡧεΫϦϓτ ( TracePoint ൛) όοΫτϨʔεใ͔Β require Λݺͼग़ͨ͠ՕॴΛಛఆ͢Δ ࠶ఆٛ͞Ε͍ͯΔͷͰ 1 ͭͣΒͯ͠ใΛऔΔ -
ϑΝΠϧؒͷґଘ୳ࡧ 2 1 TracePoint.trace(:call) do |tp| 2 next if tp.method_id
!= :require 3 4 bl = caller_locations[1] 5 feature = tp.binding.local_variable_get(tp.parameters.first.last) 6 7 puts "#{bl.path} --> #{feature}" 8 end ґଘϑΝΠϧ୳ࡧεΫϦϓτ ( TracePoint ൛) TracePoint#parameters ͱ Binding#local_variable_get Λ Έ߹Θͤͯ require Ͱࢦఆͨ͠ϥΠϒϥϦ໊Λऔಘ -
ϑΝΠϧؒͷґଘ୳ࡧ 2 1 TracePoint.trace(:call) do |tp| 2 next if tp.method_id
!= :require 3 4 bl = caller_locations[1] 5 feature = tp.binding.local_variable_get(tp.parameters.first.last) 6 7 puts "#{bl.path} --> #{feature}" 8 end ґଘϑΝΠϧ୳ࡧεΫϦϓτ ( TracePoint ൛) ࠷ޙʹ݁ՌΛग़ྗͯ͠ऴΘΓ -
ϑΝΠϧؒͷґଘ୳ࡧ 2 1 fname = 'set' 2 require fname 3
4 require %w(s e t).join 5 6 eval('require "set"') example.rb example.rb --> set example.rb --> set (eval) --> set -
ϑΝΠϧؒͷґଘ୳ࡧશ൛ (load_tracer) https://github.com/siman-man/load_tracer ϑΝΠϧؒͷґଘؔΛௐΔ gem ɾRUBY_VERSION >= '2.7.0' ͕ඞཁ 1
require 'load_tracer' 2 3 puts LoadTracer.trace(format: :dot) { require 'prime' } $ ruby load_tracer.rb | dot -Tpng -o prime_dep.png $ open prime_dep.png -
Ruby ίʔυ ·ͱΊ ߏจ ISeq τʔΫϯྻ TracePoint ߏจΛར༻ͨ͠ gem ଟ͍͕ɺTracePoint
ISeq Λར༻ͨ͠ gem গͳ͍ ੩తղੳ ಈతղੳ -
Ruby ίʔυ ·ͱΊ ߏจ ISeq τʔΫϯྻ TracePoint ݸਓతʹ ISeq ΛγϛϡϨʔτͯ͠৭ʑϑοΫΛೖΕͨ
StaticTracePoint Έ͍ͨͳͷ͕͋Δͱ໘നͦ͏ͩͱࢥ͏ɻ ੩తղੳ ಈతղੳ StaticTracePoint