50
@markbates

Mangling Ruby with TracePoint

  • Upload
    mark

  • View
    1.195

  • Download
    1

Embed Size (px)

DESCRIPTION

Presented at RubyConf 11/10/2013 Introduced in Ruby 2.0, TracePoint is meant to help developers better instrument their code for debugging and performance reasons, but there's more to TracePoint than that! In this talk we'll learn about TracePoint while building several example projects. Once we know the basics we'll use TracePoint to do things to Ruby that we couldn't have done otherwise. By the end of this talk you'll be able to frighten and amaze your friends when you show them things like true abstract classes and interfaces in Ruby, just like Java! Yikes!

Citation preview

Page 1: Mangling Ruby with TracePoint

@markbates

Page 2: Mangling Ruby with TracePoint
Page 3: Mangling Ruby with TracePoint
Page 4: Mangling Ruby with TracePoint

http://mo.markbates.com

Page 5: Mangling Ruby with TracePoint
Page 6: Mangling Ruby with TracePoint

RUBYCONF13 http://www.metacasts.tv

Page 7: Mangling Ruby with TracePoint

Mangling Ruby with TracePoint

Page 8: Mangling Ruby with TracePoint

TracePoint

Page 9: Mangling Ruby with TracePoint
Page 10: Mangling Ruby with TracePoint

trace = TracePoint.new(:raise) do |tp| p [tp.lineno, tp.event, tp.raised_exception] end #=> #<TracePoint:0x007f786a452448> !trace.enable #=> #<TracePoint:0x007f786a452448> !0 / 0 #=> [5, :raise, #<ZeroDivisionError: divided by 0>]

Page 11: Mangling Ruby with TracePoint

Events

Page 12: Mangling Ruby with TracePoint

TracePoint.new(:class) TracePoint.new(:end)

Page 13: Mangling Ruby with TracePoint

TracePoint.new(:call) TracePoint.new(:return) TracePoint.new(:c_call) TracePoint.new(:c_return)

Page 14: Mangling Ruby with TracePoint

TracePoint.new(:raise)

Page 15: Mangling Ruby with TracePoint

TracePoint.new(:b_call) TracePoint.new(:b_return)

Page 16: Mangling Ruby with TracePoint

TracePoint.new(:thread_begin) TracePoint.new(:thread_end)

Page 17: Mangling Ruby with TracePoint

Demo

Page 18: Mangling Ruby with TracePoint

Method Call Collector

Page 19: Mangling Ruby with TracePoint
Page 20: Mangling Ruby with TracePoint
Page 21: Mangling Ruby with TracePoint
Page 22: Mangling Ruby with TracePoint
Page 23: Mangling Ruby with TracePoint
Page 24: Mangling Ruby with TracePoint

TracePoint: disable 1 enable 1 !Object: puts 1 at_exit 1 !Class: new 1 method_added 1 inherited 1 !Foo: bar 1 initialize 1 !IO: write 2 puts 1 !Symbol: to_proc 1 !Array: map 1

------------- Totals: Class 3 IO 3 Object 2 TracePoint 2 Foo 2 Symbol 1 Array 1 !! Total Method Calls: 14

Page 25: Mangling Ruby with TracePoint
Page 26: Mangling Ruby with TracePoint

Total Method Calls: 122538

Page 27: Mangling Ruby with TracePoint

Class 25888 Rails::Initializable::Initializer 16502 Symbol 14136 Module 12727 String 11379 Array 6080 Hash 4479 Fixnum 3108 Object 2499 ActionDispatch::Journey::Visitors::Each 2276 File 1888 Rails::Paths::Path 1655 Regexp 1190 ThreadSafe::Cache 1026

Page 28: Mangling Ruby with TracePoint

HelloController: class 13 process_action 7 lookup_context 4 initialize 4 instance_variable_defined? 4 method_for_action 3 respond_to? 3 render 3 _normalize_options 3 render_to_body 3 _process_options 3 block_given? 2 dispatch 2 view_renderer 2 _render_template 2 process 2 append_info_to_payload 2 _prefixes 2 response_body= 2 config 2 instance_variable_get 2 _normalize_args 2 cleanup_view_runtime 2

Page 29: Mangling Ruby with TracePoint
Page 30: Mangling Ruby with TracePoint
Page 31: Mangling Ruby with TracePoint

Demo

Page 32: Mangling Ruby with TracePoint
Page 33: Mangling Ruby with TracePoint
Page 34: Mangling Ruby with TracePoint

jRuby

Page 35: Mangling Ruby with TracePoint

Abstract Interfaces IN RUBY!

Page 36: Mangling Ruby with TracePoint

Please Don’t!Actually Do This!

Page 37: Mangling Ruby with TracePoint
Page 38: Mangling Ruby with TracePoint

interface Bicycle { void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement); } !public class ACMEBicycle implements Bicycle { public void changeGear(int newValue) { // do some work here } public void speedUp(int increment) { // do some work here } public void applyBrakes(int decrement) { // do some work here } }

Page 39: Mangling Ruby with TracePoint

module ApiInterface include AbstractInterface abstract_method :get, :put end !class HttpLibrary include ApiInterface end

Page 40: Mangling Ruby with TracePoint

module ApiInterface include AbstractInterface abstract_method :get, :put end !class HttpLibrary include ApiInterface end

Page 41: Mangling Ruby with TracePoint

module ApiInterface include AbstractInterface abstract_method :get, :put end !class HttpLibrary include ApiInterface def get end def put end end

Page 42: Mangling Ruby with TracePoint
Page 43: Mangling Ruby with TracePoint
Page 44: Mangling Ruby with TracePoint
Page 45: Mangling Ruby with TracePoint
Page 46: Mangling Ruby with TracePoint
Page 47: Mangling Ruby with TracePoint
Page 48: Mangling Ruby with TracePoint
Page 49: Mangling Ruby with TracePoint

module ApiInterface include AbstractInterface abstract_method :get, :put end !class HttpLibrary include ApiInterface end

Page 50: Mangling Ruby with TracePoint

@markbates http://www.metacasts.tv