SlideShare a Scribd company logo
HIJACKING RUBY SYNTAX
IN RUBY
RubyConf 2018 LA
2018/11/15 (Thu)
@joker1007 & @tagomoris
Self-Intro Joker
▸ id: joker1007
▸ Repro inc. CTO
▸ I’m familiar with Ruby/Rails/Fluentd/ECS/Presto.
▸ This talk is my first speech abroad!!
Satoshi Tagomori (@tagomoris)
Fluentd, MessagePack-Ruby, Norikra, Woothee, ...
Arm Ltd.
Hijacking Ruby Syntax in Ruby (RubyConf 2018)
Agenda
▸ Ruby Features
▸ binding
▸ Tracepoint
▸ method hook
▸ Hacks
▸ finalist, overrider, abstriker
▸ with_resources, deferral
Ruby Features: Binding
Binding
▸ Context object, includes:
▸ Kernel#binding receiver
▸ local variables
▸ For template engines (?)
▸ Methods:
▸ #receiver, #eval,

#local_variables,

#local_variable_get,

#local_variable_defined?,

#local_variable_set
Binding creates new object per call
Setting variables are ignored on binding
Overwriting existing variable is effective!
Ruby Features: Binding
Binding#local_variable_set
▸ Method to
▸ add a variable only in a binding instance
▸ overwrite values of existing variables in original
context
Ruby Features: TracePoint
TracePoint
▸ Tracing events in VM
▸ and call hook block
▸ For various events:
▸ :line , :raise
▸ :class , :end
▸ :call , :return , :c_call , :c_return , :b_call , :b_return
▸ :thread_begin , :thread_end , :fiber_switch
▸ "We can use TracePoint to gather information specifically for
exceptions:" (from Ruby doc)
▸ This is COMPLETELY WRONG statement...
Ruby Features: TracePoint
TracePoint interrupted the argument (stringified) and return value (upcased)
Hijacking Ruby Syntax in Ruby (RubyConf 2018)
Ruby Features: TracePoint
TracePoint Methods
▸ Methods:
▸ Control: disable, enable, enabled?
▸ Event what/where: event, defined_class, path, lineno
▸ Method names: method_id, callee_id
▸ Event special: raised_exception, return_value
▸ And: binding
▸ So what?
▸ We can use TracePoint 
▸ to gather information
▸ to overwrite everything
テキスト
Break (10min)
Ruby Features: Refinements
▸ Refinements provide a way to extend a class locally
▸ Useful use case. (Safety monkey patching)
Ruby Features: Refinements
Another use case
▸ Super private method
Ruby Features: Refinements
Ruby Features: Method Hooks
▸ Ruby has six method hooks
▸ Module#method_added
▸ Module#method_removed
▸ Module#method_undefined
▸ BasicObject#singleton_method_added
▸ BasicObject#singleton_method_removed
▸ BasicObject#singleton_method_undefined
Ruby Features: method hooks
Sample: #method_added
Ruby Features: method hooks
Method hook provides a way to implement method modifier.
Hacks: Method modifiers
▸ final (https://github.com/joker1007/finalist)
▸ forbid method override
▸ override (https://github.com/joker1007/overrider)
▸ enforce method has super method
▸ abstract (https://github.com/joker1007/abstriker)
▸ enforce method override
These method modifiers work when Ruby defines class.
It is runtime, but in most case, before main logic.
Hacks: method modifiers
Sample code: finalist
Hacks: method modifiers
Hacks: method modifiers
Sample code: abstriker
Hacks: method modifiers
How to implement method modifiers
I use so many hook methods.
#included, #extended, #method_added, and TracePoint.
And I use Ripper.
In other words, I use the power of many black magics !!
Hacks: method modifiers
Use case of method_added in finalist
Ruby Features: method hooks
MyObject
Finalist
def method_added
def verify_final_method
▸ #method_added checks violations
call
Limitation of Method Hooks
▸ But it is not enough to implement “finalist” actually
▸ Ruby has so many cases of method definition
▸ def or #define_method
▸ #include module
▸ #extend, #prepend
▸ Each case has dedicated hooks
Ruby Features: method hooks
#include changes only chain of method discovering
Foo
Object
Bar
Insert module to hierarchy
It is different from method adding
Class#ancestors displays class-module hierarchy
Ruby Features: method hooks
Hooks in finalist gem
hook method override event by
method_added subclass
singleton_method_added subclass class methods
included included modules
extended extended moduled
And I talk about TracePoint too
overrider and abstriker use TracePoint
▸ #inherited and #included to start TracePoint
Tracepoint in overrider, abstriker
MyObject
Overrider/Abstriker
def included(or inherited)
TracePoint.trace(:end, :c_return, :raise)
call
Tracepoint in overrider, abstriker
TracePoint Hook
Overrider
self.override_methods = [
:method_a,
:method_b,
]
▸ Use Method#super_method method to check method
existence (ex. method(:method_a).super_method)
Why use TracePoint?
▸ In order to verify method existence at the end of class
definition.
▸ Ruby interpreter needs to wait until the end of class
definition to know a method absence.
▸ override and abstract cannot detect violation just when
they are called.
▸ In ruby, The only way to detect the violation is
TracePoint.
Advanced TracePoint: Detect particular class end
Advanced Tracepoint
:end event cannot trace definition by `Class.new`.
Use :c_return and return_value

to detect particular class end
Advanced TracePoint: Ripper combination
Advanced Tracepoint
▸ Ripper:
▸ a standard library, a parser for Ruby code
▸ outputs token list and S-expression
▸ S-expression is similar to AST
▸ has token string and token position
▸ Future option: RubyVM::AbstractSyntaxTree (2.6.0 or later)
Ripper sample:
Advanced Tracepoint
Advanced TracePoint: Ripper combination
Advanced Tracepoint
Detect target Sexp node by TracePoint#lineno
Sexp node type expresses the style of method cal
Ripper empowers TracePoint
▸ Conclusion:
▸ TracePoint detects events and where it occurs
▸ Ripper.sexp provides how methods were called
▸ Other use case
▸ power_assert gem also uses this combination
Advanced Tracepoint
Black Magic is dangerous actually,
but it is very fun,
and it extends Ruby potential
These gems are proof of concepts,
But these are decent practical.
Ruby Quiz
Break (25-30min)
What the difference between:
- #undef_method
- #remove_method
RUBY QUIZ
class Foo
def foo
class Foo
def foo
class Bar < Foo
def foo
class Bar < Foo
def foo
Bar.new.foo()
RUBY QUIZ
class Foo
def foo
class Foo
def foo
class Bar < Foo
class Bar < Foo
Bar.new.foo()
remove_method(:foo)
def foo
NoMethodError
undef_method(:foo)
Hack: with_resources
Add "with" Statement to Ruby
▸ Safe resource allocate/release
▸ Ensure to release resources
▸ at the end of a lexical scope
▸ in reverse order of allocation
▸ Idioms used very frequently
▸ Other languages:
▸ Java:

try-with-resources
▸ Python: with
▸ C#: using
Hack: with_resources
Safe Resource Allocation/Release Statement in Ruby
▸ Open method with blocks
▸ File.open(path){|f| ... }
▸ Ruby way (?)
▸ More indentation
▸ Not implemented sometimes

(e.g., TCPSocket)
Hack: with_resources
with_resources.gem
▸ Safe resource allocation
▸ Top level #with
▸ by Kernel refinement
▸ Resource allocation as lambda
▸ Multi statements to allocate resources
▸ to release first resource

if second resource allocation raises exception
▸ Block to define scope for resources
https://github.com/tagomoris/with_resources
Hack: with_resources
Implementing #with in Ruby
▸ TracePoint
▸ "b_return": pass allocated resources to block arguments
▸ "line": identify allocated resources in lambda
▸ Binding
▸ detect newly defined

local variables

in allocation lambda
▸ Refinements
▸ introduce #with

in top-level without side effects
https://github.com/tagomoris/with_resources
Hack: deferral
Alternative: defer?
▸ Multi-step resource

allocation in a method
▸ Nesting! w/ #with
▸ not so bad
▸ many nesting looks

a bit messy :(
▸ Alternative?
▸ defer in golang
Hack: deferral
Adding #defer to Ruby
▸ Block based #defer
▸ Should work
▸ Requires 1-level

nesting *always*
▸ Defer.start, end (+ nesting)

look too much (than golang)
▸ Abnormal case:

reassigning variables
Hack: deferral
deferral.gem
▸ Safe resource release
▸ Top level #defer
▸ by Kernel refinements
▸ Deferred processing to release resources
▸ at the end of scope (method, block)
▸ or exception raised
Hack: deferral
Implementing "defer" in Ruby
▸ #defer
▸ Enable TracePoint if not yet
▸ Initialize internal stack frame
▸ TracePoint
▸ Monitor method call stack
▸ Get the snapshot of local variables in defer block
▸ Call release blocks at the end of scope
▸ Binding
▸ save/restore local variables of release block
▸ Refinements
▸ introduce #defer in top-level without side effects
stack level 0
stack level 1
The Hard Thing
in Magical World:
Debugging!
"Give yourself to the dark side.
It is the only way you can save
your (Ruby)friends." - Darth vader
Thank You!
@joker1007 & @tagomoris

More Related Content

PDF
Hijacking Ruby Syntax in Ruby
PDF
RubyGems 3 & 4
PDF
How to Begin to Develop Ruby Core
PDF
Large-scaled Deploy Over 100 Servers in 3 Minutes
PDF
Middleware as Code with mruby
PDF
Middleware as Code with mruby
PDF
What's new in RubyGems3
PDF
RubyGems 3 & 4
Hijacking Ruby Syntax in Ruby
RubyGems 3 & 4
How to Begin to Develop Ruby Core
Large-scaled Deploy Over 100 Servers in 3 Minutes
Middleware as Code with mruby
Middleware as Code with mruby
What's new in RubyGems3
RubyGems 3 & 4

What's hot (20)

PDF
How to distribute Ruby to the world
PDF
Dependency Resolution with Standard Libraries
PDF
The Future of Dependency Management for Ruby
PDF
How to distribute Ruby to the world
PDF
The Future of Bundled Bundler
PDF
OSS Security the hard way
PDF
Gems on Ruby
PDF
The Future of library dependency management of Ruby
PDF
Gemification for Ruby 2.5/3.0
PDF
Roadmap for RubyGems 4 and Bundler 3
PDF
An introduction and future of Ruby coverage library
PDF
How to Begin Developing Ruby Core
PDF
Gemification for Ruby 2.5/3.0
PDF
Gemification plan of Standard Library on Ruby
PDF
The Future of library dependency manageement of Ruby
PDF
Ruby Security the Hard Way
PDF
How to test code with mruby
PDF
The secret of programming language development and future
ODP
Deploying Perl apps on dotCloud
PDF
JRuby 9000 - Taipei Ruby User's Group 2015
How to distribute Ruby to the world
Dependency Resolution with Standard Libraries
The Future of Dependency Management for Ruby
How to distribute Ruby to the world
The Future of Bundled Bundler
OSS Security the hard way
Gems on Ruby
The Future of library dependency management of Ruby
Gemification for Ruby 2.5/3.0
Roadmap for RubyGems 4 and Bundler 3
An introduction and future of Ruby coverage library
How to Begin Developing Ruby Core
Gemification for Ruby 2.5/3.0
Gemification plan of Standard Library on Ruby
The Future of library dependency manageement of Ruby
Ruby Security the Hard Way
How to test code with mruby
The secret of programming language development and future
Deploying Perl apps on dotCloud
JRuby 9000 - Taipei Ruby User's Group 2015
Ad

Similar to Hijacking Ruby Syntax in Ruby (RubyConf 2018) (20)

PPT
Ruby for C# Developers
PDF
Rapid Development with Ruby/JRuby and Rails
PPT
PDF
Low-Maintenance Perl
PDF
Pourquoi ruby et rails déchirent
PPT
Ruby Hell Yeah
PDF
Hacking with ruby2ruby
ODP
Debugging Rails 3 Applications
PDF
Ruby — An introduction
PDF
Ruby Programming Introduction
KEY
Ruby on Rails Training - Module 1
PDF
Jruby a Pi and a database
PPTX
Random Ruby Tips - Ruby Meetup 27 Jun 2018
PPT
name name2 n2.ppt
PPT
ppt2
PPT
name name2 n
PPT
ppt30
PPT
ppt18
PPT
Ruby for Perl Programmers
Ruby for C# Developers
Rapid Development with Ruby/JRuby and Rails
Low-Maintenance Perl
Pourquoi ruby et rails déchirent
Ruby Hell Yeah
Hacking with ruby2ruby
Debugging Rails 3 Applications
Ruby — An introduction
Ruby Programming Introduction
Ruby on Rails Training - Module 1
Jruby a Pi and a database
Random Ruby Tips - Ruby Meetup 27 Jun 2018
name name2 n2.ppt
ppt2
name name2 n
ppt30
ppt18
Ruby for Perl Programmers
Ad

More from SATOSHI TAGOMORI (20)

PDF
Ractor's speed is not light-speed
PDF
Good Things and Hard Things of SaaS Development/Operations
PDF
Maccro Strikes Back
PDF
Invitation to the dark side of Ruby
PDF
Make Your Ruby Script Confusing
PDF
Lock, Concurrency and Throughput of Exclusive Operations
PDF
Data Processing and Ruby in the World
PDF
Planet-scale Data Ingestion Pipeline: Bigdam
PDF
Technologies, Data Analytics Service and Enterprise Business
PDF
Ruby and Distributed Storage Systems
PDF
Perfect Norikra 2nd Season
PDF
Fluentd 101
PDF
To Have Own Data Analytics Platform, Or NOT To
PDF
The Patterns of Distributed Logging and Containers
PDF
How To Write Middleware In Ruby
PDF
Modern Black Mages Fighting in the Real World
PDF
Open Source Software, Distributed Systems, Database as a Cloud Service
PDF
Fluentd Overview, Now and Then
PDF
How to Make Norikra Perfect
PDF
Distributed Logging Architecture in Container Era
Ractor's speed is not light-speed
Good Things and Hard Things of SaaS Development/Operations
Maccro Strikes Back
Invitation to the dark side of Ruby
Make Your Ruby Script Confusing
Lock, Concurrency and Throughput of Exclusive Operations
Data Processing and Ruby in the World
Planet-scale Data Ingestion Pipeline: Bigdam
Technologies, Data Analytics Service and Enterprise Business
Ruby and Distributed Storage Systems
Perfect Norikra 2nd Season
Fluentd 101
To Have Own Data Analytics Platform, Or NOT To
The Patterns of Distributed Logging and Containers
How To Write Middleware In Ruby
Modern Black Mages Fighting in the Real World
Open Source Software, Distributed Systems, Database as a Cloud Service
Fluentd Overview, Now and Then
How to Make Norikra Perfect
Distributed Logging Architecture in Container Era

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
DOCX
The AUB Centre for AI in Media Proposal.docx
PPT
Teaching material agriculture food technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
cuic standard and advanced reporting.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
KodekX | Application Modernization Development
PDF
Modernizing your data center with Dell and AMD
PDF
Encapsulation theory and applications.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
A Presentation on Artificial Intelligence
“AI and Expert System Decision Support & Business Intelligence Systems”
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The AUB Centre for AI in Media Proposal.docx
Teaching material agriculture food technology
NewMind AI Weekly Chronicles - August'25 Week I
Dropbox Q2 2025 Financial Results & Investor Presentation
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation_ Review paper, used for researhc scholars
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Unlocking AI with Model Context Protocol (MCP)
Mobile App Security Testing_ A Comprehensive Guide.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
cuic standard and advanced reporting.pdf
20250228 LYD VKU AI Blended-Learning.pptx
KodekX | Application Modernization Development
Modernizing your data center with Dell and AMD
Encapsulation theory and applications.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
A Presentation on Artificial Intelligence

Hijacking Ruby Syntax in Ruby (RubyConf 2018)

  • 1. HIJACKING RUBY SYNTAX IN RUBY RubyConf 2018 LA 2018/11/15 (Thu) @joker1007 & @tagomoris
  • 2. Self-Intro Joker ▸ id: joker1007 ▸ Repro inc. CTO ▸ I’m familiar with Ruby/Rails/Fluentd/ECS/Presto. ▸ This talk is my first speech abroad!!
  • 3. Satoshi Tagomori (@tagomoris) Fluentd, MessagePack-Ruby, Norikra, Woothee, ... Arm Ltd.
  • 5. Agenda ▸ Ruby Features ▸ binding ▸ Tracepoint ▸ method hook ▸ Hacks ▸ finalist, overrider, abstriker ▸ with_resources, deferral
  • 6. Ruby Features: Binding Binding ▸ Context object, includes: ▸ Kernel#binding receiver ▸ local variables ▸ For template engines (?) ▸ Methods: ▸ #receiver, #eval,
 #local_variables,
 #local_variable_get,
 #local_variable_defined?,
 #local_variable_set
  • 7. Binding creates new object per call
  • 8. Setting variables are ignored on binding
  • 10. Ruby Features: Binding Binding#local_variable_set ▸ Method to ▸ add a variable only in a binding instance ▸ overwrite values of existing variables in original context
  • 11. Ruby Features: TracePoint TracePoint ▸ Tracing events in VM ▸ and call hook block ▸ For various events: ▸ :line , :raise ▸ :class , :end ▸ :call , :return , :c_call , :c_return , :b_call , :b_return ▸ :thread_begin , :thread_end , :fiber_switch ▸ "We can use TracePoint to gather information specifically for exceptions:" (from Ruby doc) ▸ This is COMPLETELY WRONG statement...
  • 13. TracePoint interrupted the argument (stringified) and return value (upcased)
  • 15. Ruby Features: TracePoint TracePoint Methods ▸ Methods: ▸ Control: disable, enable, enabled? ▸ Event what/where: event, defined_class, path, lineno ▸ Method names: method_id, callee_id ▸ Event special: raised_exception, return_value ▸ And: binding ▸ So what? ▸ We can use TracePoint  ▸ to gather information ▸ to overwrite everything
  • 17. Ruby Features: Refinements ▸ Refinements provide a way to extend a class locally ▸ Useful use case. (Safety monkey patching) Ruby Features: Refinements
  • 18. Another use case ▸ Super private method Ruby Features: Refinements
  • 19. Ruby Features: Method Hooks ▸ Ruby has six method hooks ▸ Module#method_added ▸ Module#method_removed ▸ Module#method_undefined ▸ BasicObject#singleton_method_added ▸ BasicObject#singleton_method_removed ▸ BasicObject#singleton_method_undefined Ruby Features: method hooks
  • 21. Method hook provides a way to implement method modifier.
  • 22. Hacks: Method modifiers ▸ final (https://github.com/joker1007/finalist) ▸ forbid method override ▸ override (https://github.com/joker1007/overrider) ▸ enforce method has super method ▸ abstract (https://github.com/joker1007/abstriker) ▸ enforce method override These method modifiers work when Ruby defines class. It is runtime, but in most case, before main logic. Hacks: method modifiers
  • 23. Sample code: finalist Hacks: method modifiers
  • 25. Sample code: abstriker Hacks: method modifiers
  • 26. How to implement method modifiers I use so many hook methods. #included, #extended, #method_added, and TracePoint. And I use Ripper. In other words, I use the power of many black magics !! Hacks: method modifiers
  • 27. Use case of method_added in finalist Ruby Features: method hooks MyObject Finalist def method_added def verify_final_method ▸ #method_added checks violations call
  • 28. Limitation of Method Hooks ▸ But it is not enough to implement “finalist” actually ▸ Ruby has so many cases of method definition ▸ def or #define_method ▸ #include module ▸ #extend, #prepend ▸ Each case has dedicated hooks Ruby Features: method hooks
  • 29. #include changes only chain of method discovering Foo Object Bar Insert module to hierarchy It is different from method adding Class#ancestors displays class-module hierarchy Ruby Features: method hooks
  • 30. Hooks in finalist gem hook method override event by method_added subclass singleton_method_added subclass class methods included included modules extended extended moduled
  • 31. And I talk about TracePoint too
  • 32. overrider and abstriker use TracePoint ▸ #inherited and #included to start TracePoint Tracepoint in overrider, abstriker MyObject Overrider/Abstriker def included(or inherited) TracePoint.trace(:end, :c_return, :raise) call
  • 33. Tracepoint in overrider, abstriker TracePoint Hook Overrider self.override_methods = [ :method_a, :method_b, ] ▸ Use Method#super_method method to check method existence (ex. method(:method_a).super_method)
  • 34. Why use TracePoint? ▸ In order to verify method existence at the end of class definition. ▸ Ruby interpreter needs to wait until the end of class definition to know a method absence. ▸ override and abstract cannot detect violation just when they are called. ▸ In ruby, The only way to detect the violation is TracePoint.
  • 35. Advanced TracePoint: Detect particular class end Advanced Tracepoint :end event cannot trace definition by `Class.new`. Use :c_return and return_value
 to detect particular class end
  • 36. Advanced TracePoint: Ripper combination Advanced Tracepoint ▸ Ripper: ▸ a standard library, a parser for Ruby code ▸ outputs token list and S-expression ▸ S-expression is similar to AST ▸ has token string and token position ▸ Future option: RubyVM::AbstractSyntaxTree (2.6.0 or later)
  • 38. Advanced TracePoint: Ripper combination Advanced Tracepoint Detect target Sexp node by TracePoint#lineno Sexp node type expresses the style of method cal
  • 39. Ripper empowers TracePoint ▸ Conclusion: ▸ TracePoint detects events and where it occurs ▸ Ripper.sexp provides how methods were called ▸ Other use case ▸ power_assert gem also uses this combination Advanced Tracepoint
  • 40. Black Magic is dangerous actually, but it is very fun, and it extends Ruby potential These gems are proof of concepts, But these are decent practical.
  • 41. Ruby Quiz Break (25-30min) What the difference between: - #undef_method - #remove_method
  • 42. RUBY QUIZ class Foo def foo class Foo def foo class Bar < Foo def foo class Bar < Foo def foo Bar.new.foo()
  • 43. RUBY QUIZ class Foo def foo class Foo def foo class Bar < Foo class Bar < Foo Bar.new.foo() remove_method(:foo) def foo NoMethodError undef_method(:foo)
  • 44. Hack: with_resources Add "with" Statement to Ruby ▸ Safe resource allocate/release ▸ Ensure to release resources ▸ at the end of a lexical scope ▸ in reverse order of allocation ▸ Idioms used very frequently ▸ Other languages: ▸ Java:
 try-with-resources ▸ Python: with ▸ C#: using
  • 45. Hack: with_resources Safe Resource Allocation/Release Statement in Ruby ▸ Open method with blocks ▸ File.open(path){|f| ... } ▸ Ruby way (?) ▸ More indentation ▸ Not implemented sometimes
 (e.g., TCPSocket)
  • 46. Hack: with_resources with_resources.gem ▸ Safe resource allocation ▸ Top level #with ▸ by Kernel refinement ▸ Resource allocation as lambda ▸ Multi statements to allocate resources ▸ to release first resource
 if second resource allocation raises exception ▸ Block to define scope for resources https://github.com/tagomoris/with_resources
  • 47. Hack: with_resources Implementing #with in Ruby ▸ TracePoint ▸ "b_return": pass allocated resources to block arguments ▸ "line": identify allocated resources in lambda ▸ Binding ▸ detect newly defined
 local variables
 in allocation lambda ▸ Refinements ▸ introduce #with
 in top-level without side effects https://github.com/tagomoris/with_resources
  • 48. Hack: deferral Alternative: defer? ▸ Multi-step resource
 allocation in a method ▸ Nesting! w/ #with ▸ not so bad ▸ many nesting looks
 a bit messy :( ▸ Alternative? ▸ defer in golang
  • 49. Hack: deferral Adding #defer to Ruby ▸ Block based #defer ▸ Should work ▸ Requires 1-level
 nesting *always* ▸ Defer.start, end (+ nesting)
 look too much (than golang) ▸ Abnormal case:
 reassigning variables
  • 50. Hack: deferral deferral.gem ▸ Safe resource release ▸ Top level #defer ▸ by Kernel refinements ▸ Deferred processing to release resources ▸ at the end of scope (method, block) ▸ or exception raised
  • 51. Hack: deferral Implementing "defer" in Ruby ▸ #defer ▸ Enable TracePoint if not yet ▸ Initialize internal stack frame ▸ TracePoint ▸ Monitor method call stack ▸ Get the snapshot of local variables in defer block ▸ Call release blocks at the end of scope ▸ Binding ▸ save/restore local variables of release block ▸ Refinements ▸ introduce #defer in top-level without side effects stack level 0 stack level 1
  • 52. The Hard Thing in Magical World: Debugging!
  • 53. "Give yourself to the dark side. It is the only way you can save your (Ruby)friends." - Darth vader Thank You! @joker1007 & @tagomoris