Upgrade to Pro — share decks privately, control downloads, hide ads and more …

現場の RubyVM::AbstractSyntaxTree

hkdnet
June 29, 2019

現場の RubyVM::AbstractSyntaxTree

TokyuRubyKaigi 13 のLTスライドです。
http://regional.rubykaigi.org/tokyu13/

---

RubyVM::AbstractSyntaxTree (AST) is a very useful tool. It is not only used for black magic. It is helpful for your Ruby projects.

hkdnet

June 29, 2019
Tweet

More Decks by hkdnet

Other Decks in Technology

Transcript

  1. ͓͜ͱΘΓ • ASTͷઆ໌͸͜ΕҎ߱Ͱ͸͠·ͤΜ • AST = Abstract Syntax Tree •

    ந৅ߏจ໦ͱݴΘΕΔ΍ͭͰύʔεͨ݁͠ՌͰ͋Γɺϓϩάϥ ϜͷҙຯΛࣔͨ͠໦ߏ଄ͩͱࢥ͍ͬͯͩ͘͞
  2. grep ͷݶք • Ruby εΫϦϓτʹ͓͍ͯɺจࣈྻ͚ͩͰ͸ҙຯΛ൑அͰ͖ͳ ͍ x = 1 #

    ͋Δ͍͸͜ͷߦͳ͠ x # ϩʔΧϧม਺ݺͼग़͠ or Ϩγʔόͳ͠ϝιουݺͼग़͠
  3. RubyVM::AbstractSyntaxTree require 'call_searcher' s = CallSearcher::Searcher.new { |e| e.mid ==

    :search } root_dir = File.expand_path('../', __dir__) result = s.search_dir(root_dir: root_dir, path: '{app,lib,spec}', github: 'hkdnet/call_searcher') result.each { |e| puts e.github_url } # => https://github.com/hkdnet/call_searcher/blob/master/ lib/call_searcher/searcher.rb#L14
  4. slim ରԠ • ύʔεͨ͠Β SࣜΛฦ͖ͯͯ͠ΊΜͲ͘͘͞ͳͬͨ parser = Slim::Parser.new parser.call(s) [:multi,

    [:html, :tag, "h1", [:html, :attrs], [:slim, :output, true, "@foo", [:multi, [:newline]]]]]
  5. haml • Haml::Parser ͕ AST Λฦ͢ͷͰ͔ͦ͜Β Ruby ͱͯ͠ධՁ͞Ε Δ΋ͷΛൈ͍ͯ͘Ε͹ྑ͍ <-

    ΢ι • લड़ͷΑ͏ʹ a ͱ͍͏εΫϦϓτ͸ a ͕ԿͰ͋Δ͔ʹΑܾͬͯ ·Δ • ݁ہίϯςΩετґଘ͕ൈ͚ͳ͍
  6. haml • ࠷ऴతʹ view Λు͘1ͭͷ Ruby εΫϦϓτʹͳΔ͸͔ͣͩΒ ͔ͦ͜Βൈ͍ͯ͘Ε͹ྑ͍ʁ • ͦΕͬͯ

    helper ͱ͔ਖ਼͘͠ೝࣝͰ͖ΔΜͩΖ͏͔ • ͳΜ͔ແཧͳؾ͕͠ͳ͘΋ͳ͍ͳʔ <- ΠϚίί