O que tem de novo no

Ruby 2?
               Fabio Akita
              @akitaonrails
          codeminer42.com
Ruby 0.95   Dez 1995

Ruby 1.8    Ago 2003

Rails 0.8   Dez 2005

Ruby 1.9    Dez 2007
Ruby 0.95   Dez 1995

Ruby 1.8    Ago 2003

Rails 0.8   Dez 2005

Ruby 1.9    Dez 2007
Ruby 0.95   Dez 1995

Ruby 1.8    Ago 2003

Rails 0.8   Dez 2005

Ruby 1.9    Dez 2007
Ruby 0.95   Dez 1995

Ruby 1.8    Ago 2003

Rails 0.8   Dez 2005

Ruby 1.9    Dez 2007
Ruby 2.0
  24 de Fevereiro de 2013
Compatível com 1.9
Literal Symbol List
> %q[hello world]
=> "hello world"

> %i[hello world]
=> [:hello, :world]
Keyword Arguments
def foo(options = {})
  param1 = options[:param1]
  param2 = options[:param2]
  return [param1, param2]
end

> foo param1: "Hello", param2: "World"
=> ["Hello", "World"]
def foo(param1: nil, param2: nil)
  return [param1, param2]
end

> foo param1: "Hello", param2: "World"
=> ["Hello", "World"]
Module Prepend
> [1,2,3].to_s
=> "[1, 2, 3]"
module Foo
  def self.included(base)
    base.class_eval do
      alias_method :original_to_s, :to_s

      def to_s
        original_to_s + ", Foo"
      end
    end
  end
end

class Array
  include Foo
end
> [1,2,3].to_s
=> "[1, 2, 3], Foo"

> Array.ancestors
=> [Array, Foo, Enumerable, Object, Kernel,
BasicObject]
module Foo
  def to_s
    super + ", Foo"
  end
end

class Array
  prepend Foo
end
> [1,2,3].to_s
=> "[1, 2, 3], Foo"

> Array.ancestors
=> [Foo, Array, Enumerable, Object, Kernel,
BasicObject]
Lazy Enumerators
> a = (0..3).map { |i| i }
=> [0, 1, 2, 3]
> a = (0..3).lazy.map { |i| i }
=> #<Enumerator::Lazy: #<Enumerator::Lazy: 0..3>:map>

> a.next
=> 0
> a.next
=> 1
> a.next
=> 2
> a.next
=> 3
> a = (0..Float::INFINITY).lazy.map { |i| i }
=> #<Enumerator::Lazy: #<Enumerator::Lazy:
0..Infinity>:map>

> a.next
=> 0
> a.next
=> 1
> a.next
=> 2
> a.next
=> 3
Refinements
module Foo
  def hello
    "Hello, #{to_s}"
  end
end

class Array
  include Foo
end

> [1,2,3].hello
=> "Hello, [1, 2, 3]"
module Foo
  refine Array do
    def hello
      "Hello, #{to_s}"
    end
  end
end

using Foo

> [1,2,3].hello
=> "Hello, [1, 2, 3]"
module Foo               def bah
  refine Array do          [1,2,3].hello
    def hello            end
      "Hello, #{to_s}"
    end                  > bah.hello
  end                    NoMethodError: undefined
end                      method `hello' for
                         "Hello, [1, 2,
using Foo                3]":String

> [1,2,3].hello
=> "Hello, [1, 2, 3]"
http://rvm.io
curl -L https://get.rvm.io | bash -s stable --rails --autolibs=enabled
http://gembundler.com/
Browser




NGINX


Worker




 Linux
Browser




NGINX


Worker




 Linux
Browser




NGINX


Worker




 Ruby


 Linux
Browser




NGINX


Worker


 Rails

 Ruby


 Linux
Browser




NGINX


Worker


 Rails

 Ruby


 Linux
Browser   Browser           Browser   Browser




                    NGINX


Worker    Worker            Worker    Worker


 Rails     Rails             Rails      Rails

Ruby       Ruby              Ruby      Ruby


                    Linux
Browser   Browser           Browser   Browser




                    NGINX


Worker    Worker            Worker    Worker


 Rails     Rails             Rails      Rails

Ruby       Ruby              Ruby      Ruby


                    Linux
M&S
Mark & Sweep GC
struct RBasic {
    VALUE flags;   RString
                    flags

    VALUE klass;    klass

};
RString   RString   RString
 flags     flags     flags

 klass     klass     klass
RString   RString   RString
 flags     flags     flags

 klass     klass     klass




          fork()
RString   RString   RString
 flags     flags     flags

 klass     klass     klass




          fork()
RString    RString     RString
 flags       flags      flags

 klass       klass      klass




          GC.start()



RString    RString     RString
 flags       flags      flags

 klass       klass      klass
OOBW
 Out of Band Work
Lazy
Lazy Sweep GC - Ruby 1.9.3
“CoW”
 Copy on Write
RString   RString   RString
 flags     flags     flags

 klass     klass     klass
RString   RString   RString
 flags     flags     flags

 klass     klass     klass
RString   RString   RString
 flags     flags     flags

 klass     klass     klass
RString   RString   RString
 flags     flags     flags

 klass     klass     klass
fork()
GC.start
Bitmap
Bitmap Marking GC - Ruby 2.0
9   00001001


8   00001000


7   00000111
9   00001001


8   00001000


7   00000111
9   00001001


8   00001000


7   00000111
00000001   2^0   1     User


00000010   2^1   2   Moderator


00000100   2^2   4 Administrator
00000001   2^0   1     User


00000010   2^1   2   Moderator


00000100   2^2   4 Administrator
00000001   2^0   1     User


00000010   2^1   2   Moderator


00000100   2^2   4 Administrator
1   1   1   0   0   0   0   0   0   0   0
1   1   1   0   0   0   0   0   0   0   0




                                            fork()
fork()
fork()
Browser   Browser           Browser   Browser




                    NGINX


Worker    Worker            Worker    Worker


 Rails     Rails             Rails      Rails

Ruby       Ruby              Ruby      Ruby


                    Linux
Browser
     Browser
          Browser
               Browser
                    Browser
                         Browser
                              Browser
                                   Browser
                                        Browser
                                             Browser




                             NGINX


  Worker Worker
      Worker Worker
                 Worker
                     Worker
                         Worker
                             Worker
                                 Worker
                                     Worker


   Rails Rails Rails Rails Rails Rails Rails Rails Rails Rails

   RubyRuby Ruby Ruby Ruby Ruby Ruby Ruby Ruby Ruby


                             Linux
Browser
     Browser
          Browser
               Browser
                    Browser
                         Browser
                              Browser
                                   Browser
                                        Browser
                                             Browser




                             NGINX


  Worker Worker
      Worker Worker
                 Worker
                     Worker
                         Worker
                             Worker
                                 Worker
                                     Worker


   Rails Rails Rails Rails Rails Rails Rails Rails Rails Rails

   RubyRuby Ruby Ruby Ruby Ruby Ruby Ruby Ruby Ruby


                             Linux
Rails 3.2.12+


  Rails 4.0
Thanks!
 @akitaonrails

O que tem de novo no Ruby 2.0?

  • 1.
    O que temde novo no Ruby 2? Fabio Akita @akitaonrails codeminer42.com
  • 4.
    Ruby 0.95 Dez 1995 Ruby 1.8 Ago 2003 Rails 0.8 Dez 2005 Ruby 1.9 Dez 2007
  • 5.
    Ruby 0.95 Dez 1995 Ruby 1.8 Ago 2003 Rails 0.8 Dez 2005 Ruby 1.9 Dez 2007
  • 6.
    Ruby 0.95 Dez 1995 Ruby 1.8 Ago 2003 Rails 0.8 Dez 2005 Ruby 1.9 Dez 2007
  • 7.
    Ruby 0.95 Dez 1995 Ruby 1.8 Ago 2003 Rails 0.8 Dez 2005 Ruby 1.9 Dez 2007
  • 8.
    Ruby 2.0 24 de Fevereiro de 2013
  • 9.
  • 10.
  • 11.
    > %q[hello world] =>"hello world" > %i[hello world] => [:hello, :world]
  • 12.
  • 13.
    def foo(options ={}) param1 = options[:param1] param2 = options[:param2] return [param1, param2] end > foo param1: "Hello", param2: "World" => ["Hello", "World"]
  • 14.
    def foo(param1: nil,param2: nil) return [param1, param2] end > foo param1: "Hello", param2: "World" => ["Hello", "World"]
  • 15.
  • 16.
  • 17.
    module Foo def self.included(base) base.class_eval do alias_method :original_to_s, :to_s def to_s original_to_s + ", Foo" end end end end class Array include Foo end
  • 18.
    > [1,2,3].to_s => "[1,2, 3], Foo" > Array.ancestors => [Array, Foo, Enumerable, Object, Kernel, BasicObject]
  • 19.
    module Foo def to_s super + ", Foo" end end class Array prepend Foo end
  • 20.
    > [1,2,3].to_s => "[1,2, 3], Foo" > Array.ancestors => [Foo, Array, Enumerable, Object, Kernel, BasicObject]
  • 21.
  • 22.
    > a =(0..3).map { |i| i } => [0, 1, 2, 3]
  • 23.
    > a =(0..3).lazy.map { |i| i } => #<Enumerator::Lazy: #<Enumerator::Lazy: 0..3>:map> > a.next => 0 > a.next => 1 > a.next => 2 > a.next => 3
  • 24.
    > a =(0..Float::INFINITY).lazy.map { |i| i } => #<Enumerator::Lazy: #<Enumerator::Lazy: 0..Infinity>:map> > a.next => 0 > a.next => 1 > a.next => 2 > a.next => 3
  • 25.
  • 26.
    module Foo def hello "Hello, #{to_s}" end end class Array include Foo end > [1,2,3].hello => "Hello, [1, 2, 3]"
  • 27.
    module Foo refine Array do def hello "Hello, #{to_s}" end end end using Foo > [1,2,3].hello => "Hello, [1, 2, 3]"
  • 28.
    module Foo def bah refine Array do [1,2,3].hello def hello end "Hello, #{to_s}" end > bah.hello end NoMethodError: undefined end method `hello' for "Hello, [1, 2, using Foo 3]":String > [1,2,3].hello => "Hello, [1, 2, 3]"
  • 29.
  • 30.
    curl -L https://get.rvm.io| bash -s stable --rails --autolibs=enabled
  • 35.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
    Browser Browser Browser Browser NGINX Worker Worker Worker Worker Rails Rails Rails Rails Ruby Ruby Ruby Ruby Linux
  • 46.
    Browser Browser Browser Browser NGINX Worker Worker Worker Worker Rails Rails Rails Rails Ruby Ruby Ruby Ruby Linux
  • 47.
  • 48.
    struct RBasic { VALUE flags; RString flags VALUE klass; klass };
  • 49.
    RString RString RString flags flags flags klass klass klass
  • 50.
    RString RString RString flags flags flags klass klass klass fork()
  • 51.
    RString RString RString flags flags flags klass klass klass fork()
  • 52.
    RString RString RString flags flags flags klass klass klass GC.start() RString RString RString flags flags flags klass klass klass
  • 53.
    OOBW Out ofBand Work
  • 56.
    Lazy Lazy Sweep GC- Ruby 1.9.3
  • 57.
  • 58.
    RString RString RString flags flags flags klass klass klass
  • 59.
    RString RString RString flags flags flags klass klass klass
  • 60.
    RString RString RString flags flags flags klass klass klass
  • 61.
    RString RString RString flags flags flags klass klass klass
  • 63.
  • 68.
  • 73.
  • 76.
    9 00001001 8 00001000 7 00000111
  • 77.
    9 00001001 8 00001000 7 00000111
  • 78.
    9 00001001 8 00001000 7 00000111
  • 80.
    00000001 2^0 1 User 00000010 2^1 2 Moderator 00000100 2^2 4 Administrator
  • 81.
    00000001 2^0 1 User 00000010 2^1 2 Moderator 00000100 2^2 4 Administrator
  • 82.
    00000001 2^0 1 User 00000010 2^1 2 Moderator 00000100 2^2 4 Administrator
  • 83.
    1 1 1 0 0 0 0 0 0 0 0
  • 84.
    1 1 1 0 0 0 0 0 0 0 0 fork()
  • 85.
  • 86.
  • 87.
    Browser Browser Browser Browser NGINX Worker Worker Worker Worker Rails Rails Rails Rails Ruby Ruby Ruby Ruby Linux
  • 88.
    Browser Browser Browser Browser Browser Browser Browser Browser Browser Browser NGINX Worker Worker Worker Worker Worker Worker Worker Worker Worker Worker Rails Rails Rails Rails Rails Rails Rails Rails Rails Rails RubyRuby Ruby Ruby Ruby Ruby Ruby Ruby Ruby Ruby Linux
  • 89.
    Browser Browser Browser Browser Browser Browser Browser Browser Browser Browser NGINX Worker Worker Worker Worker Worker Worker Worker Worker Worker Worker Rails Rails Rails Rails Rails Rails Rails Rails Rails Rails RubyRuby Ruby Ruby Ruby Ruby Ruby Ruby Ruby Ruby Linux
  • 90.
    Rails 3.2.12+ Rails 4.0
  • 91.