Mangling Ruby with TracePoint

Preview:

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

@markbates

http://mo.markbates.com

RUBYCONF13 http://www.metacasts.tv

Mangling Ruby with TracePoint

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>]

Events

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

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

TracePoint.new(:raise)

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

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

Demo

Method Call Collector

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

Total Method Calls: 122538

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

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

Demo

jRuby

Abstract Interfaces IN RUBY!

Please Don’t!Actually Do This!

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 } }

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

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

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

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

@markbates http://www.metacasts.tv

Recommended