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

Ruby 2.0 (en)

Ruby 2.0 (en)

Ruby 2.0 at Waza 2013 (English version)

Yukihiro Matsumoto

March 01, 2013
Tweet

More Decks by Yukihiro Matsumoto

Other Decks in Technology

Transcript

  1. Powered by Rabbit 1.0.4 Ruby 2.0 Chief Architect, Ruby @

    Heroku @yukihiro_matz Yukihiro "Matz" Matsumoto
  2. Powered by Rabbit 1.0.4   Dec 1996 1.0 Aug 1997

    1.1 Dec 1998 1.2 Aug 1999 1.4 Sep 2000 1.6 17/143
  3. Powered by Rabbit 1.0.4   Aug 2003 1.8 Dec 2007

    1.9.0 Aug 2010 1.9.2 Oct 2011 1.9.3 18/143
  4. Powered by Rabbit 1.0.4 New Hash literals {:foo => 1,

    :bar => 2} as {foo: 1, bar: 2} 58/143
  5. Powered by Rabbit 1.0.4 New features in 2.0 Keyword arguments

    Module#prepend Enumerable#lazy Refinements 63/143
  6. Powered by Rabbit 1.0.4 New features in 2.0 symbol array

    literals to_h conversion method UTF-8 by default Dtrace / TracePoint 64/143
  7. Powered by Rabbit 1.0.4 Keyword Arguments Named optinal arguments No

    specific order Descriptive Easy-to-remember 66/143
  8. Powered by Rabbit 1.0.4 Keyword Arguments in 1.9 def log(msg,

    opt = {}) level = opt[:level] || "ERROR" time = opt[:time] || Time.now puts "#{ time.ctime } [#{ level }] #{ msg }" end 68/143
  9. Powered by Rabbit 1.0.4 Keyword Arguments in 1.9 What if

    you want to combin with arbitrary number of arguments descriptive exceptions pass nil as a valid value 69/143
  10. Powered by Rabbit 1.0.4 Keyword Arguments in 1.9 def log(*msgs)

    opt = msgs.last.is_a?(Hash) ? msgs.pop : {} level = opt.key?(:level) ? opt.delete(:level) : "ERROR" time = opt.key?(:time) ? opt.delete(:time) : Time.now raise "unknown keyword: #{ opt.keys.first }" if !opt.empty? msgs.each {|msg| puts "#{ time.ctime } [#{ level }] #{ msg }" } end 70/143
  11. Powered by Rabbit 1.0.4 Keyword Arguments in 2.0 def log(msg,

    level: "ERROR", time: Time.now) puts "#{ time.ctime } [#{ level }] #{ msg }" end 71/143
  12. Powered by Rabbit 1.0.4 Keyword Arguments in 2.0 Simpler More

    descriptive API Easy to read Easy to write 72/143
  13. Powered by Rabbit 1.0.4 Keyword Arguments in 2.0 log("Hello!", **opt)

    You can pass existing hash as keyword arguments 73/143
  14. Powered by Rabbit 1.0.4 Keyword Arguments in 2.0 def log(msg,

    level: "ERROR", time: Time.now, **kw) puts "#{ time.ctime } [#{ level }] #{ msg }" end You can get keyword arguments as a hash Unkown keywords do not raise error 74/143
  15. Powered by Rabbit 1.0.4 Keyword Arguments in 2.0 Summary Easy

    API More descriptive Rails prefer keyword arguments 75/143
  16. Powered by Rabbit 1.0.4 alias method chain class A def

    foo; puts "foo"; end end class A # reopen def foo_with_bar foo_without_bar puts "bar" end alias_method_chain :foo, :bar end A.new.foo 79/143
  17. Powered by Rabbit 1.0.4 alias method chain def alias_method_chain(target, feature)

    alias_method "#{target}_without_#{feature}", target alias_method target, "#{target}_with_#{feature}" end 80/143
  18. Powered by Rabbit 1.0.4 alias method chain problems many public

    methods spilled out accidental feature name conflict adding same feature set to classes 81/143
  19. Powered by Rabbit 1.0.4 Method combination from CLOS(CommonLisp Object System)

    to extend existing methods confugurable via MOP 82/143
  20. Powered by Rabbit 1.0.4 Module#prepend Methods added by #include comes

    after exisiting methods #prepend put them before to wrap methods 87/143
  21. Powered by Rabbit 1.0.4 Module#prepend class Foo def foo; p

    :foo; end end module Prepend def foo p :before super p :after end end class Foo prepend Prepend end Foo.new.foo 88/143
  22. Powered by Rabbit 1.0.4 Module#prepend Summary Extending exising methods No

    method name conflict Package features in a module 89/143
  23. Powered by Rabbit 1.0.4 Monkey Patching Open class Reopen existing

    class/module Adding methods Replacing methods 92/143
  24. Powered by Rabbit 1.0.4 Refinements module R refine String do

    def foo ... end end end "".foo # => error! using R "".foo 103/143
  25. Powered by Rabbit 1.0.4 Selector namespace No one knows Smallscript

    Smallscript still alive? Very complex 105/143
  26. Powered by Rabbit 1.0.4 Functional programming wannabe Immutable data Pattern

    matching Lazy evaluation Function composition 112/143
  27. Powered by Rabbit 1.0.4 Lazy evaluation What if we had

    lazy version of enumerating methods 116/143
  28. Powered by Rabbit 1.0.4 to_i and to_int to_i for explicit

    conversion to_int for implicit conversion 127/143