SlideShare a Scribd company logo
1 of 33
Download to read offline
Invitation to
the Dark Side of Ruby:
Maccro Strikes Back
#tqrk13

Satoshi Tagomori (@tagomoris)
Satoshi Tagomori (@tagomoris)

Fluentd, MessagePack-Ruby, Norikra, Woothee, ...

Arm Ltd.
"If you only knew the power of the dark side"
<< >>
Vote!!!!!!
Maccro
Demonstration
Maccro: register
• Register rules with "before" and "after" matchers

• "before"/"after": code in Ruby, with DSL for code placeholders

• Placeholders (N: integer, >= 1):

• eN: expressions which returns a value or values

• vN: a single value (e.g., variable, constant, literal, etc)
Maccro: Apply on Methods
• Macro rules applied to methods

• manually: apply(ModName,
ModName.method(:method_name)

• automatically: using TracePoint (:end)
• Registered matchers:

• replace placeholders w/ matcher nodes for
RubyVM::AST::Node
Maccro: Prepare
VCALL: e1 VCALL: e2
VCALL: e3
Matcher: e1 Matcher: e2
Matcher: e3
OPCALL: <
OPCALL: <
OPCALL: <
OPCALL: <
Matcher code
e1 < e2 < e3
Placeholders
Matcher tree
• Applied methods:

• parsed into AST using
RubyVM::AbstractSyntaxTree#of

• get the source of the method
#<UnboundMethod: Mod#foo>
def foo(v)
if bar(v) < 1
# ...
elsif bar(v) == 1
# ...
elsif 1 < bar(v) < 2
# ...
else
# ...
Maccro: Match to Method
def foo(v)
if bar(v) < 1
# ...
elsif bar(v) == 1
# ...
elsif 1 < bar(v) < 2
# ...
else
# ...
end
end
• Matched tree:

• get code snippets for matcher nodes

• e1: 1 (literal)

• e2: bar(v) (func call with v)

• e3: 2 (literal)
e1 < e2 < e3
Matcher: e1 Matcher: e2
Matcher: e3
Maccro: Rewrite code (1)
• Matched tree:

• replace patterns in "after" code with captured snippets

• e1: 1 (literal)

• e2: bar(v)

• e3: 2 (literal)
e1 < e2 && e2 < e3
"After" code
1 < bar(v) && bar(v) < 2
"After" code for this method
"Before" code
e1 < e2 < e3
Maccro: Rewrite code (2)
• Matched tree:

• rewrite original method code

with updated "after" code
def foo(v)
if bar(v) < 1
# ...
elsif bar(v) == 1
# ...
elsif 1 < bar(v) < 2
# ...
else
# ...
end
end
e1 < e2 && e2 < e3
"After" code
1 < bar(v) && bar(v) < 2
"After" code for this method
"Before" code
e1 < e2 < e3
Original code of this method
Maccro: Rewrite code (3)
Maccro: Rewrite code (3)
• Matched tree:

• rewrite original method code

with updated "after" code
1 < bar(v) && bar(v) < 2
"After" code for this method
def foo(v)
if bar(v) < 1
# ...
elsif bar(v) == 1
# ...
elsif 1 < bar(v) && bar(v) < 2
# ...
else
# ...
end
end
Updated code of this method
e1 < e2 && e2 < e3
"After" code
"Before" code
e1 < e2 < e3
• Rewritten method source:

• call Modue#module_eval to replace the method
definition
def foo(v)
if bar(v) < 1
# ...
elsif bar(v) == 1
# ...
elsif 1 < bar(v) < 2
# ...
else
# ...
end
end
def foo(v)
if bar(v) < 1
# ...
elsif bar(v) == 1
# ...
elsif 1 < bar(v) && bar(v) < 2
# ...
else
# ...
end
end
Overwrite the method definition
by Module#module_eval
Maccro: Rewrite Method
Nice stuffs
• Less performance penalty

• Macro processor works only when methods are defined

• Ruby version independent Macro rules

• Compatible between Ruby versions

• Because of rules written in Ruby code

• Built-in rules, useful for some cases

• Continuous inequality operators (<=, <, >, >=)

• ActiveRecord utilities
• Supported only on Ruby 2.6 or later

• Only code in def in module/class are rewritable

• Calling class method just after definition can't be updated

• Same method can't be rewritten twice

• Non-idempotent methods causes unexpected result

• Updating Lambda and local variable name matching is not implemented yet

• Stack trace of updated methods would be confusing

• Source from STDIN or command line (and irb/pry) cause errors

• Rule-order-dependent issues will be caused

• Method visibility are not taken care right now

• Non-self singleton method definition causes errors

• Placeholder validations are not implemented yet
Limitations
Just Few Limitations
at April (RubyKaigi 2019)
Limitations
• Supported only on Ruby 2.6 or later

• Calling class method just after definition can't be updated

• Non-idempotent methods causes unexpected result

• Stack trace of updated methods would be confusing

• Source from STDIN or command line (and irb/pry) cause errors

• Rule-order-dependent issues will be caused

• Method visibility are not taken care right now

• Non-self singleton method definition causes errors

• Placeholder validations are not implemented yet
Just Few Limitations (now)
Maccro v0.2.0
• Support to rewrite (+call) any lambda/proc code

• Support new placeholders

for strings, symbols, numbers and regexps
#tqrk13

Satoshi Tagomori (@tagomoris)
Rails
• ActiveRecord ! 

• I WANT YOU
"Join me and I will complete your training."
<< >>
https://github.com/tagomoris/maccro
https://rubygems.org/gems/maccro
• 

• #tqrk13 2
• 

• limited 

• 3kg
4kg
• 1kg → 

• 1kg → 

• 2kg →
• 

• #asakusarb

•
tqrk14... ?

More Related Content

What's hot

Jslab rssh: JS as language platform
Jslab rssh:  JS as language platformJslab rssh:  JS as language platform
Jslab rssh: JS as language platform
Ruslan Shevchenko
 
20140419 oedo rubykaigi04
20140419 oedo rubykaigi0420140419 oedo rubykaigi04
20140419 oedo rubykaigi04
Hiroshi SHIBATA
 
20140425 ruby conftaiwan2014
20140425 ruby conftaiwan201420140425 ruby conftaiwan2014
20140425 ruby conftaiwan2014
Hiroshi SHIBATA
 

What's hot (20)

Lightbend Lagom: Microservices Just Right
Lightbend Lagom: Microservices Just RightLightbend Lagom: Microservices Just Right
Lightbend Lagom: Microservices Just Right
 
WTF is Twisted?
WTF is Twisted?WTF is Twisted?
WTF is Twisted?
 
An Introduction to Twisted
An Introduction to TwistedAn Introduction to Twisted
An Introduction to Twisted
 
Twisted Introduction
Twisted IntroductionTwisted Introduction
Twisted Introduction
 
#Pharo Days 2016 Reflectivity
#Pharo Days 2016 Reflectivity#Pharo Days 2016 Reflectivity
#Pharo Days 2016 Reflectivity
 
The Actor Model - Towards Better Concurrency
The Actor Model - Towards Better ConcurrencyThe Actor Model - Towards Better Concurrency
The Actor Model - Towards Better Concurrency
 
Scala io2013 : Our journey from UML/MDD to Scala macros
Scala io2013 : Our journey from UML/MDD to Scala macrosScala io2013 : Our journey from UML/MDD to Scala macros
Scala io2013 : Our journey from UML/MDD to Scala macros
 
Actors and Threads
Actors and ThreadsActors and Threads
Actors and Threads
 
Logging in Scala
Logging in ScalaLogging in Scala
Logging in Scala
 
From 'Legacy' to 'Edge'
From 'Legacy' to 'Edge'From 'Legacy' to 'Edge'
From 'Legacy' to 'Edge'
 
Oslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep DiveOslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep Dive
 
Jslab rssh: JS as language platform
Jslab rssh:  JS as language platformJslab rssh:  JS as language platform
Jslab rssh: JS as language platform
 
Introduction to Actor Model and Akka
Introduction to Actor Model and AkkaIntroduction to Actor Model and Akka
Introduction to Actor Model and Akka
 
#Pharo Days 2016 Data Formats and Protocols
#Pharo Days 2016 Data Formats and Protocols#Pharo Days 2016 Data Formats and Protocols
#Pharo Days 2016 Data Formats and Protocols
 
How we use Twisted in Launchpad
How we use Twisted in LaunchpadHow we use Twisted in Launchpad
How we use Twisted in Launchpad
 
Lock-free algorithms for Kotlin Coroutines
Lock-free algorithms for Kotlin CoroutinesLock-free algorithms for Kotlin Coroutines
Lock-free algorithms for Kotlin Coroutines
 
Variables in Pharo5
Variables in Pharo5Variables in Pharo5
Variables in Pharo5
 
20140419 oedo rubykaigi04
20140419 oedo rubykaigi0420140419 oedo rubykaigi04
20140419 oedo rubykaigi04
 
How does Scala's Future work?
How does Scala's Future work?How does Scala's Future work?
How does Scala's Future work?
 
20140425 ruby conftaiwan2014
20140425 ruby conftaiwan201420140425 ruby conftaiwan2014
20140425 ruby conftaiwan2014
 

Similar to Maccro Strikes Back

RailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMsRailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMs
Lourens Naudé
 
Counters with Riak on Amazon EC2 at Hackover
Counters with Riak on Amazon EC2 at HackoverCounters with Riak on Amazon EC2 at Hackover
Counters with Riak on Amazon EC2 at Hackover
Andrei Savu
 

Similar to Maccro Strikes Back (20)

Invitation to the dark side of Ruby
Invitation to the dark side of RubyInvitation to the dark side of Ruby
Invitation to the dark side of Ruby
 
Behavioral Reflection in Pharo
Behavioral Reflection in PharoBehavioral Reflection in Pharo
Behavioral Reflection in Pharo
 
What's a macro?: Learning by Examples
What's a macro?: Learning by ExamplesWhat's a macro?: Learning by Examples
What's a macro?: Learning by Examples
 
Bytecode Manipulation with a Java Agent and Byte Buddy
Bytecode Manipulation with a Java Agent and Byte BuddyBytecode Manipulation with a Java Agent and Byte Buddy
Bytecode Manipulation with a Java Agent and Byte Buddy
 
Type Profiler: Ambitious Type Inference for Ruby 3
Type Profiler: Ambitious Type Inference for Ruby 3Type Profiler: Ambitious Type Inference for Ruby 3
Type Profiler: Ambitious Type Inference for Ruby 3
 
Game of Performance: A Song of JIT and GC
Game of Performance: A Song of JIT and GCGame of Performance: A Song of JIT and GC
Game of Performance: A Song of JIT and GC
 
RailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMsRailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMs
 
Triton and Symbolic execution on GDB@DEF CON China
Triton and Symbolic execution on GDB@DEF CON ChinaTriton and Symbolic execution on GDB@DEF CON China
Triton and Symbolic execution on GDB@DEF CON China
 
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
 
Code lifecycle in the jvm - TopConf Linz
Code lifecycle in the jvm - TopConf LinzCode lifecycle in the jvm - TopConf Linz
Code lifecycle in the jvm - TopConf Linz
 
Memcached-инъекции - они существуют и работают, Иван Новиков (ONsec)
Memcached-инъекции - они существуют и работают, Иван Новиков (ONsec)Memcached-инъекции - они существуют и работают, Иван Новиков (ONsec)
Memcached-инъекции - они существуют и работают, Иван Новиков (ONsec)
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond Smalltak
 
VMs, Interpreters, JIT
VMs, Interpreters, JITVMs, Interpreters, JIT
VMs, Interpreters, JIT
 
PHP Forum Paris 2012: Magic behind the numbers. Software metrics in practice
PHP Forum Paris 2012: Magic behind the numbers. Software metrics in practicePHP Forum Paris 2012: Magic behind the numbers. Software metrics in practice
PHP Forum Paris 2012: Magic behind the numbers. Software metrics in practice
 
Djangocon
DjangoconDjangocon
Djangocon
 
TypeProf for IDE: Enrich Development Experience without Annotations
TypeProf for IDE: Enrich Development Experience without AnnotationsTypeProf for IDE: Enrich Development Experience without Annotations
TypeProf for IDE: Enrich Development Experience without Annotations
 
Sista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performanceSista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performance
 
Counters with Riak on Amazon EC2 at Hackover
Counters with Riak on Amazon EC2 at HackoverCounters with Riak on Amazon EC2 at Hackover
Counters with Riak on Amazon EC2 at Hackover
 
Postgres for MySQL (and other database) people
Postgres for MySQL (and other database) peoplePostgres for MySQL (and other database) people
Postgres for MySQL (and other database) people
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond Smalltak
 

More from SATOSHI TAGOMORI

More from SATOSHI TAGOMORI (20)

Ractor's speed is not light-speed
Ractor's speed is not light-speedRactor's speed is not light-speed
Ractor's speed is not light-speed
 
Good Things and Hard Things of SaaS Development/Operations
Good Things and Hard Things of SaaS Development/OperationsGood Things and Hard Things of SaaS Development/Operations
Good Things and Hard Things of SaaS Development/Operations
 
Hijacking Ruby Syntax in Ruby (RubyConf 2018)
Hijacking Ruby Syntax in Ruby (RubyConf 2018)Hijacking Ruby Syntax in Ruby (RubyConf 2018)
Hijacking Ruby Syntax in Ruby (RubyConf 2018)
 
Make Your Ruby Script Confusing
Make Your Ruby Script ConfusingMake Your Ruby Script Confusing
Make Your Ruby Script Confusing
 
Hijacking Ruby Syntax in Ruby
Hijacking Ruby Syntax in RubyHijacking Ruby Syntax in Ruby
Hijacking Ruby Syntax in Ruby
 
Lock, Concurrency and Throughput of Exclusive Operations
Lock, Concurrency and Throughput of Exclusive OperationsLock, Concurrency and Throughput of Exclusive Operations
Lock, Concurrency and Throughput of Exclusive Operations
 
Data Processing and Ruby in the World
Data Processing and Ruby in the WorldData Processing and Ruby in the World
Data Processing and Ruby in the World
 
Planet-scale Data Ingestion Pipeline: Bigdam
Planet-scale Data Ingestion Pipeline: BigdamPlanet-scale Data Ingestion Pipeline: Bigdam
Planet-scale Data Ingestion Pipeline: Bigdam
 
Technologies, Data Analytics Service and Enterprise Business
Technologies, Data Analytics Service and Enterprise BusinessTechnologies, Data Analytics Service and Enterprise Business
Technologies, Data Analytics Service and Enterprise Business
 
Ruby and Distributed Storage Systems
Ruby and Distributed Storage SystemsRuby and Distributed Storage Systems
Ruby and Distributed Storage Systems
 
Perfect Norikra 2nd Season
Perfect Norikra 2nd SeasonPerfect Norikra 2nd Season
Perfect Norikra 2nd Season
 
Fluentd 101
Fluentd 101Fluentd 101
Fluentd 101
 
To Have Own Data Analytics Platform, Or NOT To
To Have Own Data Analytics Platform, Or NOT ToTo Have Own Data Analytics Platform, Or NOT To
To Have Own Data Analytics Platform, Or NOT To
 
The Patterns of Distributed Logging and Containers
The Patterns of Distributed Logging and ContainersThe Patterns of Distributed Logging and Containers
The Patterns of Distributed Logging and Containers
 
How To Write Middleware In Ruby
How To Write Middleware In RubyHow To Write Middleware In Ruby
How To Write Middleware In Ruby
 
Modern Black Mages Fighting in the Real World
Modern Black Mages Fighting in the Real WorldModern Black Mages Fighting in the Real World
Modern Black Mages Fighting in the Real World
 
Open Source Software, Distributed Systems, Database as a Cloud Service
Open Source Software, Distributed Systems, Database as a Cloud ServiceOpen Source Software, Distributed Systems, Database as a Cloud Service
Open Source Software, Distributed Systems, Database as a Cloud Service
 
Fluentd Overview, Now and Then
Fluentd Overview, Now and ThenFluentd Overview, Now and Then
Fluentd Overview, Now and Then
 
How to Make Norikra Perfect
How to Make Norikra PerfectHow to Make Norikra Perfect
How to Make Norikra Perfect
 
Distributed Logging Architecture in Container Era
Distributed Logging Architecture in Container EraDistributed Logging Architecture in Container Era
Distributed Logging Architecture in Container Era
 

Recently uploaded

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Recently uploaded (20)

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 

Maccro Strikes Back

  • 1. Invitation to the Dark Side of Ruby: Maccro Strikes Back #tqrk13 Satoshi Tagomori (@tagomoris)
  • 2. Satoshi Tagomori (@tagomoris) Fluentd, MessagePack-Ruby, Norikra, Woothee, ... Arm Ltd.
  • 3.
  • 4. "If you only knew the power of the dark side" << >>
  • 8. Maccro: register • Register rules with "before" and "after" matchers • "before"/"after": code in Ruby, with DSL for code placeholders • Placeholders (N: integer, >= 1): • eN: expressions which returns a value or values • vN: a single value (e.g., variable, constant, literal, etc)
  • 9. Maccro: Apply on Methods • Macro rules applied to methods • manually: apply(ModName, ModName.method(:method_name) • automatically: using TracePoint (:end)
  • 10. • Registered matchers: • replace placeholders w/ matcher nodes for RubyVM::AST::Node Maccro: Prepare VCALL: e1 VCALL: e2 VCALL: e3 Matcher: e1 Matcher: e2 Matcher: e3 OPCALL: < OPCALL: < OPCALL: < OPCALL: < Matcher code e1 < e2 < e3 Placeholders Matcher tree
  • 11. • Applied methods: • parsed into AST using RubyVM::AbstractSyntaxTree#of • get the source of the method #<UnboundMethod: Mod#foo> def foo(v) if bar(v) < 1 # ... elsif bar(v) == 1 # ... elsif 1 < bar(v) < 2 # ... else # ... Maccro: Match to Method
  • 12. def foo(v) if bar(v) < 1 # ... elsif bar(v) == 1 # ... elsif 1 < bar(v) < 2 # ... else # ... end end • Matched tree: • get code snippets for matcher nodes • e1: 1 (literal) • e2: bar(v) (func call with v) • e3: 2 (literal) e1 < e2 < e3 Matcher: e1 Matcher: e2 Matcher: e3 Maccro: Rewrite code (1)
  • 13. • Matched tree: • replace patterns in "after" code with captured snippets • e1: 1 (literal) • e2: bar(v) • e3: 2 (literal) e1 < e2 && e2 < e3 "After" code 1 < bar(v) && bar(v) < 2 "After" code for this method "Before" code e1 < e2 < e3 Maccro: Rewrite code (2)
  • 14. • Matched tree: • rewrite original method code
 with updated "after" code def foo(v) if bar(v) < 1 # ... elsif bar(v) == 1 # ... elsif 1 < bar(v) < 2 # ... else # ... end end e1 < e2 && e2 < e3 "After" code 1 < bar(v) && bar(v) < 2 "After" code for this method "Before" code e1 < e2 < e3 Original code of this method Maccro: Rewrite code (3)
  • 15. Maccro: Rewrite code (3) • Matched tree: • rewrite original method code
 with updated "after" code 1 < bar(v) && bar(v) < 2 "After" code for this method def foo(v) if bar(v) < 1 # ... elsif bar(v) == 1 # ... elsif 1 < bar(v) && bar(v) < 2 # ... else # ... end end Updated code of this method e1 < e2 && e2 < e3 "After" code "Before" code e1 < e2 < e3
  • 16. • Rewritten method source: • call Modue#module_eval to replace the method definition def foo(v) if bar(v) < 1 # ... elsif bar(v) == 1 # ... elsif 1 < bar(v) < 2 # ... else # ... end end def foo(v) if bar(v) < 1 # ... elsif bar(v) == 1 # ... elsif 1 < bar(v) && bar(v) < 2 # ... else # ... end end Overwrite the method definition by Module#module_eval Maccro: Rewrite Method
  • 17. Nice stuffs • Less performance penalty • Macro processor works only when methods are defined • Ruby version independent Macro rules • Compatible between Ruby versions • Because of rules written in Ruby code • Built-in rules, useful for some cases • Continuous inequality operators (<=, <, >, >=) • ActiveRecord utilities
  • 18. • Supported only on Ruby 2.6 or later • Only code in def in module/class are rewritable • Calling class method just after definition can't be updated • Same method can't be rewritten twice • Non-idempotent methods causes unexpected result • Updating Lambda and local variable name matching is not implemented yet • Stack trace of updated methods would be confusing • Source from STDIN or command line (and irb/pry) cause errors • Rule-order-dependent issues will be caused • Method visibility are not taken care right now • Non-self singleton method definition causes errors • Placeholder validations are not implemented yet Limitations Just Few Limitations at April (RubyKaigi 2019)
  • 19. Limitations • Supported only on Ruby 2.6 or later • Calling class method just after definition can't be updated • Non-idempotent methods causes unexpected result • Stack trace of updated methods would be confusing • Source from STDIN or command line (and irb/pry) cause errors • Rule-order-dependent issues will be caused • Method visibility are not taken care right now • Non-self singleton method definition causes errors • Placeholder validations are not implemented yet Just Few Limitations (now)
  • 20. Maccro v0.2.0 • Support to rewrite (+call) any lambda/proc code • Support new placeholders
 for strings, symbols, numbers and regexps
  • 21.
  • 22.
  • 23.
  • 24.
  • 26.
  • 27. Rails • ActiveRecord ! • I WANT YOU
  • 28. "Join me and I will complete your training." << >> https://github.com/tagomoris/maccro https://rubygems.org/gems/maccro
  • 30. • • limited • 3kg
  • 31. 4kg • 1kg → • 1kg → • 2kg →