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

Ruby 2.0 Walkthrough: The Best Bits

Ruby 2.0 Walkthrough: The Best Bits

A quick overview of a few of the best bits in Ruby 2.0.

Peter Cooper

February 25, 2013
Tweet

More Decks by Peter Cooper

Other Decks in Programming

Transcript

  1. RUBY 2.0
    WALKTHROUGH

    View Slide

  2. The Best Bits
    THE RUBY 2.0 RUNTHROUGH

    View Slide

  3. A sprint, not a stroll

    View Slide

  4. BIG PARTS

    View Slide

  5. Refinements
    Experimental scope-limited
    monkey patching

    View Slide

  6. module FloatDivision
    refine Fixnum do
    def /(other); self.to_f / other; end
    end
    end
    class MathFun
    using FloatDivision
    def self.ratio(a, b)
    a / b
    end
    end
    p MathFun.ratio(6, 8)
    THE OLD IDEA (no longer works)

    View Slide

  7. module FloatDivision
    refine Fixnum do
    def /(other); self.to_f / other; end
    end
    end
    class MathFun
    using FloatDivision
    def self.ratio(a, b)
    a / b
    end
    end
    p MathFun.ratio(6, 8)

    View Slide

  8. module FloatDivision
    refine Fixnum do
    def /(other); self.to_f / other; end
    end
    end
    using FloatDivision
    class MathFun
    def self.ratio(a, b)
    a / b
    end
    end
    p MathFun.ratio(6, 8)
    Main context is OK

    View Slide

  9. Keyword Arguments
    definitely a: “pretty”, good:
    “addition”, to: “the language”

    View Slide

  10. def some_method(options = {})
    defaults = { x: 10, y: 20, z: 30 }
    options = defaults.merge(options)
    p options
    end
    some_method x: 1, y: 2

    View Slide

  11. def some_method(x: 10, y: 20, z: 30)
    p x, y, z
    end
    some_method x: 1, y: 2, z: 3

    View Slide

  12. def some_method(x: 10, **rest)
    p x, rest
    end
    some_method x: 1, y: 2, z: 3, c: “x”

    View Slide

  13. Enumerator::Lazy
    Lazy enumeration, finally made easy.

    View Slide

  14. infinite_range = (0..Float::INFINITY)
    infinite_range.select { |num| num % 74 == 0 }

    View Slide

  15. infinite_range = (0..Float::INFINITY)
    e = infinite_range.lazy.select { |num| num % 74 == 0 }
    puts e.next
    puts e.next
    puts e.next

    View Slide

  16. Module#prepend

    View Slide

  17. module Bar
    def my_method
    "inside the module"
    end
    end
    class Foo
    include Bar
    def my_method
    "inside the class"
    super
    end
    end
    x = Foo.new
    p x.my_method
    1
    2

    View Slide

  18. module Bar
    def my_method
    "inside the module"
    super
    end
    end
    class Foo
    prepend Bar
    def my_method
    "inside the class"
    end
    end
    x = Foo.new
    p x.my_method
    1
    2

    View Slide

  19. INCLUDE PREPEND
    Foo class
    Bar module
    x object
    NEITHER
    Foo class
    x object
    Object class
    Object class
    Foo class
    Bar module
    x object
    Object class

    View Slide

  20. DETAILS

    View Slide

  21. ABI Breakage
    Changes to the Application Binary
    Interface

    View Slide

  22. Regex engine changed
    From Oniguruma to Onigmo

    View Slide

  23. %{this\r\nis\n\ncool\vhello!}.gsub(/\R/, '')

    View Slide

  24. RubyGems 2.0
    Lots of refactoring, initial support
    for stdlib gemification, gem search
    is now remote, arbitrary
    metadata support & more.

    View Slide

  25. RDoc 4.0
    Adds Markdown support & ri can
    now show pages (e.g. READMEs
    with ri rdoc:README)

    View Slide

  26. %i and %I
    To form arrays of symbols, like %w
    does for words and arrays

    View Slide

  27. %i{a b c} == [:a, :b, :c]
    %I{ #{(rand(26) + 65).chr} } == [:Y]

    View Slide

  28. UTF-8 is default
    source encoding
    No more # encoding: utf-8 (if you
    don’t want)

    View Slide

  29. Bitmap garbage
    collector
    Faster, more efficient

    View Slide

  30. CSV.load and
    CSV.dump gone
    Not considered such a good idea
    in light of the early 2013 YAML
    vulnerabilities

    View Slide

  31. String#chars, #lines,
    #codepoints, etc.
    Now return arrays rather than
    enumerators.
    Use #each_char, #each_line, etc.
    instead if you need enumerators.

    View Slide

  32. TracePoint
    A more object oriented alternative
    to set_trace_func

    View Slide

  33. tracer = lambda do |event, file, line, id, binding, klass|
    to_display = [event, File.basename(file), line, klass, id]
    puts "%10s in %s at line %-2d %s:%s" % to_display
    end
    set_trace_func tracer
    # .. normal code here ..

    View Slide

  34. tracer = TracePoint.new do |tp|
    to_display = [tp.event, File.basename(tp.path), tp.lineno,
    tp.defined_class, tp.method_id]
    puts "%10s in %s at line %-2d %s:%s" % to_display
    end
    tracer.enable
    # .. normal code here ..

    View Slide

  35. respond_to?
    respond_to? against a protected
    method now returns false

    View Slide

  36. Method transplants
    with define_method
    Now accepts UnboundMethods

    View Slide

  37. module M
    def foo; "foo"; end
    end
    define_method :foo, M.instance_method(:foo)
    p foo

    View Slide

  38. Array#bsearch
    Range#bsearch
    Binary search for monotonic
    collections

    View Slide

  39. [1, 4, 9, 13, 14, 22, 40].bsearch { |i| p i; i >= 6 }
    # 13
    # 4
    # 9
    # => 9
    (1..1000).bsearch { |i| i > 372 }

    View Slide

  40. __dir__
    Like __FILE__ but for the current
    source file’s directory
    Equivalent to
    File.dirname(File.realpath(__FILE__))

    View Slide

  41. to_h and Hash()
    A new convention. The hash equivalent
    to #to_a
    A key use is with Struct and OpenStruct

    View Slide

  42. User = Struct.new(:name, :age, :status)
    me = User.new("Peter", 31, :admin)
    me.to_h
    ENV.to_h
    Hash(nil) # => {}
    Hash([]) # => {}

    View Slide

  43. FIN
    https://cooperpress.com/

    View Slide