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

Ruby 2.0 upcoming changes

Hemant Kumar
January 21, 2013
110

Ruby 2.0 upcoming changes

A very brief look at ruby 2.0 upcoming changes

Hemant Kumar

January 21, 2013
Tweet

Transcript

  1. List of things • Refinements • Keyword arguments • Module#prepend

    • DTrace Support • TracePoint Monday, 21 January 13
  2. New way module FixnumExtras refine Fixnum do def hours_ago Time.now

    - self*3600 end end end > 10.hours_ago Undefined method > using FixnumExtras > 10.hours_ago Monday, 21 January 13
  3. Another use case? module FixnumExtras refine Fixnum do def hours_ago

    Time.now - self*3600 end end end class Foo using FixnumExtras end class Bar < Foo def hello 10.hours_ago end end a = Bar.new() a.hello() Monday, 21 January 13
  4. Wrong module FixnumExtras refine Fixnum do def hours_ago Time.now -

    self*3600 end end end class Foo using FixnumExtras end class Bar < Foo def hello 10.hours_ago end end a = Bar.new() a.hello() Monday, 21 January 13
  5. Mind bender • using can only be called on main

    object. module StringExtras refine String do def wow puts "Called wow" end end end using StringExtras class Foo def hello "hello".wow() end end a = Foo.new() a.hello Monday, 21 January 13
  6. More mind benders • It is limited to file scope

    • scope begins where using is called and ends at the end of file. • It is confusing as hell and may change in future. Monday, 21 January 13
  7. Keyword arguments def link_to(url, css_klass: 'foo', css_id: 'foo') %Q{<a href="#{url}"

    class=#{css_klass} id=#{css_id} />} end link_to("http://www.google.com", css_klass: "foo", css_id: "bar") Monday, 21 January 13
  8. def link_to(url, options = {} ) css_klass = options[:css_klass] css_id

    = options[:css_id] %Q{<a href="#{url}" class=#{css_klass} id=#{css_id} />} end link_to("http://www.google.com", css_klass: "foo", css_id: "bar") def link_to(url, css_klass: 'foo', css_id: 'foo') %Q{<a href="#{url}" class=#{css_klass} id=#{css_id} />} end link_to("http://www.google.com", css_klass: "foo", css_id: "bar") Monday, 21 January 13
  9. Pains it solves • alias_method_chain • remove_method and redefine the

    method • always define API methods of your class in modules. Monday, 21 January 13
  10. Usage class Foo def hello puts "hello from foo" end

    end class Bar < Foo def hello puts "hello from bar" end end Monday, 21 January 13
  11. Usage class Foo def hello puts "hello from foo" end

    end class Bar < Foo def hello puts "hello from bar" end end class Foo def hello puts "overridden definition of hello" end end Monday, 21 January 13
  12. Usage class Foo def hello puts "hello from foo" end

    end class Bar < Foo def hello puts "hello from bar" end end class Foo def hello puts "overridden definition of hello" end end module FooExtensions def self.included(base) base.undef_method :hello end def hello puts "Hello from Foo extensions" end end Foo.send(:include,FooExtensions) Monday, 21 January 13
  13. New way class Foo def hello puts "hello from foo"

    end end module FooExtensions def hello puts "Hello from Foo extensions" end end Foo.send(:prepend, FooExtensions) a = Foo.new() a.hello() Monday, 21 January 13
  14. TracePoint trace = TracePoint.new(:call) do |tp| p [tp.lineno, tp.defined_class, tp.method_id,

    tp.event] end trace.enable MoreComplex.new().start() Monday, 21 January 13