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

Ruby 2 Roundup

Ruby 2 Roundup

Presentation for Ruby FTW (http://rubyftw.org)

Avatar for Phillip Ridlen

Phillip Ridlen

March 25, 2013
Tweet

Other Decks in Technology

Transcript

  1. UTF-8 by default ‘ ˒  ⽁   ✚

    ⚔ ✔ ✖ ✿ ✯ ✱ ❖ ⦨ ’ ― ˑ ⚄ ֓צ ⌘ 㿀  ✉ ©
  2. def fooify(name, options={}) mortal = options[:mortal] || true hungry =

    options[:hunrgy] || false angry = options[:mortal] || false Foo.new(name, mortal, hungry, angry) end
  3. def fooify(name, options={}) mortal = options[:mortal] || true hungry =

    options[:hunrgy] || false angry = options[:mortal] || false Foo.new(name, mortal, hungry, angry) end
  4. # with rails def fooify(name, options={}) options.reverse_merge!({ name: name, mortal:

    true, hungry: false, angry: false }) Foo.new(name, mortal, hungry, angry) end
  5. # with rails def fooify(name, options={}) options.reverse_merge!({ name: name, mortal:

    true, hungry: false, angry: false }) Foo.new(name, mortal, hungry, angry) end
  6. # ruby 2.0 def fooify(name, mortal: true, hungry: false, angry:

    false) Foo.new(name, mortal, hungry, angry) end
  7. # ruby 2.0 def fooify(name, mortal: true, hungry: false, angry:

    false, **rest) Foo.new(name, mortal, hungry, angry) puts rest # => { key: ‘val’, key2: ‘val2’ } end
  8. class Foo def bar “I’m Foo” end end module FooChanger

    def bar_too “#{bar_orig} new” end def self.included(base) base.class_eval do alias_method :bar_orig, :bar alias_method :bar, :bar_too end end end Foo.send :include, FooChanger
  9. class Foo def bar “I’m Foo” end end module Changer

    def bar_with_changer “#{bar_without_changer} new” end def self.included(base) base.class_eval do alias_method_chain :bar, :changer end end end Foo.send :include, Changer
  10. class Foo def bar “I’m Foo” end end module FooChanger

    def bar “#{super} new” end end Foo.send :prepend, FooChanger
  11. module Bar def bar "bar" end def baz "baz" end

    end Bar.instance_method(:bar).bind(Object.new).call #=> "bar"
  12. module Bar def bar "bar" end def baz "baz" end

    end class Foo def bar “foobar” end def baz “foobaz” end end
  13. barbar = Bar.instance_method(:bar) #=> #<UnboundMethod: Bar#bar> foo = Foo.new foobar

    = barbar.bind(foo) #=> #<Method: Foo(Bar)#bar> foo.bar #=> “bar” foo.baz #=> “foobaz” foobar #=> “foobar”
  14. barbar = Bar.instance_method(:bar) #=> #<UnboundMethod: Bar#bar> foo = Foo.new foobar

    = barbar.bind(foo) #=> #<Method: Foo(Bar)#bar> foo.bar #=> “bar” foo.baz #=> “foobaz” foobar #=> “foobar”
  15. barbar = Bar.instance_method(:bar) #=> #<UnboundMethod: Bar#bar> foo = Foo.new foobar

    = barbar.bind(foo) #=> #<Method: Foo(Bar)#bar> foo.bar #=> “bar” foo.baz #=> “foobaz” foobar #=> “foobar”
  16. barbar = Bar.instance_method(:bar) #=> #<UnboundMethod: Bar#bar> foo = Foo.new foobar

    = barbar.bind(foo) #=> #<Method: Foo(Bar)#bar> foo.bar #=> “bar” foo.baz #=> “foobaz” foobar #=> “foobar”
  17. barbar = Bar.instance_method(:bar) #=> #<UnboundMethod: Bar#bar> foo = Foo.new foobar

    = barbar.bind(foo) #=> #<Method: Foo(Bar)#bar> foo.bar #=> “bar” foo.baz #=> “foobaz” foobar #=> “foobar”
  18. barbar = Bar.instance_method(:bar) #=> #<UnboundMethod: Bar#bar> foo = Foo.new foobar

    = barbar.bind(foo) #=> #<Method: Foo(Bar)#bar> foo.bar #=> “bar” foo.baz #=> “foobaz” foobar #=> “foobar”
  19. module Kernel def from(mod, include: []) unless Module === mod

    raise TypeError, "arg must be a mod" include.each do |name, orig| define_method(name, mod.instance_method(orig || name)) end end end
  20. module Bar def bar; “bar”; end def baz; “baz”; end

    end class Foo from Bar, include: {:qux => :bar} end f = Foo.new f.qux #=> "bar f.respond_to?(:baz) #=> false
  21. __dir__ # Ruby 1.9 require File.dirname(__FILE__) + ‘lib/rubyftw’ # Ruby

    2.0 require __dir__ + ‘lib/rubyftw’ # BONUS! Ruby 1.9 require_relative ‘lib/rubyftw’
  22. • CGI supports HTML5 – cgi = CGI.new(“html5”) • No

    more Iconv, use String#encode instead
  23. • CGI supports HTML5 – cgi = CGI.new(“html5”) • No

    more Iconv, use String#encode instead • Syck is gone
  24. • CGI supports HTML5 – cgi = CGI.new(“html5”) • No

    more Iconv, use String#encode instead • Syck is gone • Hash access for OpenStruct OpenStruct.new(foo: “test”)[:foo] #=> “test”
  25. • CGI supports HTML5 – cgi = CGI.new(“html5”) • No

    more Iconv, use String#encode instead • Syck is gone • Hash access for OpenStruct OpenStruct.new(foo: “test”)[:foo] #=> “test” • Thread improvements (local vars, Mutex, more)
  26. • CGI supports HTML5 – cgi = CGI.new(“html5”) • No

    more Iconv, use String#encode instead • Syck is gone • Hash access for OpenStruct OpenStruct.new(foo: “test”)[:foo] #=> “test” • Thread improvements (local vars, Mutex, more) • GC improvements
  27. • No warning for unused vars starting with “_” •

    define_method available for use at the top level • And much, much more...