$30 off During Our Annual Pro Sale. View Details »

Ruby 2.1 Overview

Ruby 2.1 Overview

(For HoustonRB)

Jesse Wolgamott

January 15, 2014
Tweet

More Decks by Jesse Wolgamott

Other Decks in Technology

Transcript

  1. @jwo
    Ruby 2.1

    View Slide

  2. @jwo
    `def` returns a
    method

    View Slide

  3. @jwo
    module Foo!
    def public_method!
    end!
    !
    def some_other_method!
    end!
    !
    private :some_other_method!
    !
    private!
    def a_private_method!
    end!
    end!
    !
    Foo.private_instance_methods!
    => [:some_other_method, :a_private_method]

    View Slide

  4. @jwo
    module Foo!
    def public_method!
    end!
    !
    private def some_other_method!
    end!
    !
    private def a_private_method!
    end!
    end!
    !
    Foo.private_instance_methods!
    => [:some_other_method, :a_private_method]

    View Slide

  5. @jwo
    Refinements
    Launched and the world did not end.

    View Slide

  6. @jwo
    module Permalinker!
    refine String do!
    def permalinkify!
    downcase.split.join("-")!
    end!
    end!
    end

    View Slide

  7. @jwo
    class Post!
    using Permalinker!
    !
    def initialize(title)!
    @title = title!
    end!
    !
    def permalink!
    @title.permalinkify!
    end!
    end

    View Slide

  8. @jwo
    "Refinements are not globally scoped".permalinkify!
    !
    !
    !
    NoMethodError: undefined method `permalinkify' for
    "Refinements are not globally scoped":String

    View Slide

  9. @jwo
    Restricted
    Keyword Args

    View Slide

  10. @jwo
    def order_tacos(type: 5, how_many: 'queso')!
    puts "Ordering #{how_many} #{type}"!
    end!
    !
    order_tacos type: "fajita_queso", how_many: 5
    in Ruby 2.0, you HAD to specify a
    default for type and how_many

    View Slide

  11. @jwo
    def order_tacos(type:, how_many:)!
    puts "Ordering #{how_many} #{type}"!
    end!
    !
    order_tacos type: "fajita_queso", how_many: 5!
    order_tacos type: “fajita_queso"!
    !
    => ArgumentError: missing keyword: how_many
    NO LONGER

    View Slide

  12. @jwo
    Restricted Generational
    Garbage Collector
    (RGENGC)

    View Slide

  13. @jwo
    ummm wat

    View Slide

  14. @jwo
    Ruby 1.8: Mark and Sweep
    http://tmm1.net/ruby21-rgengc/

    View Slide

  15. @jwo
    Ruby 1.9.3 “Lazy Sweep”
    http://tmm1.net/ruby21-rgengc/

    View Slide

  16. @jwo
    Ruby 2.0 bitmaps for copy-on-write
    safety
    http://tmm1.net/ruby21-rgengc/

    View Slide

  17. @jwo
    Ruby 2.1: OldGen and minor marking
    http://tmm1.net/ruby21-rgengc/

    View Slide

  18. @jwo
    Some Graphs!

    View Slide

  19. @jwo
    Some Little Stuff

    View Slide

  20. @jwo
    27r # => Rational(27/1)!
    1/2r # => Rational(1/2)!
    12+33i # => Complex(12,33)
    Decimal Literal Syntax

    View Slide

  21. @jwo
    3.times{puts'blah'.object_id}!
    # => 70287241728160!
    # => 70287241728080!
    # => 70287241728020!
    !
    3.times{puts'blah'f.object_id}!
    # => 70287241709760 !
    # => 70287241709760!
    # => 70287241709760
    Frozen String Literal
    (Same)

    View Slide

  22. @jwo
    $ stackprof data/stackprof-cpu-4120-1384979644.dump --text --limit 4
    ==================================
    Mode: cpu(1000)
    Samples: 9145 (1.25% miss rate)
    GC: 448 (4.90%)
    ==================================
    TOTAL (pct) SAMPLES (pct) FRAME
    236 (2.6%) 231 (2.5%) String#blank?
    546 (6.0%) 216 (2.4%) ActiveRecord::ConnectionAdapters::Mysql2Adapter#select
    212 (2.3%) 199 (2.2%) Mysql2::Client#query_with_timing
    !
    $ stackprof data/stackprof-cpu-4120-1384979644.dump --method 'String#blank?'
    String#blank? (lib/active_support/core_ext/object/blank.rb:80)
    samples: 231 self (2.5%) / 236 total (2.6%)
    callers:
    112 ( 47.5%) Object#present?
    code:
    | 80 | def blank?
    187 (2.0%) / 187 (2.0%) | 81 | self !~ /[^[:space:]]/
    Profiling Tools (rb_profile_frames)

    View Slide

  23. @jwo
    Install today!

    View Slide