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

Ruby 2.1

Ruby 2.1

Gave this talk during January 2014 Singapore Ruby Brigade meetup.

Benjamin Tan Wei Hao

January 21, 2014
Tweet

More Decks by Benjamin Tan Wei Hao

Other Decks in Programming

Transcript

  1. What's New?! 1.  Rational Number & Complex ! Number Literals

    ! 2.  def‘s return value! 3.  Refinements! 4.  Required Keyword Arguments! 5.  Garbage Collector! 6.  Object Allocation Tracing!
  2. What's New?! 1.  Rational Number & Complex ! Number Literals

    ! 2.  def‘s return value! 3.  Refinements! 4.  Required Keyword Arguments! 5.  Garbage Collector! 6.  Object Allocation Tracing!
  3. Complex Literals! > (2 + 3i)! => (2+3i)! > (2

    + 3i) + Complex(5, 4i)! => (3+3i)! > Complex(2, 3)! => (2+3i)! < Ruby 2.1 Ruby 2.1
  4. Rational Literals! > 2/3r + 5/4r! => (23/12)! > 2/3.0

    + 5/4.0! => 1.91666666666665! < Ruby 2.1 Ruby 2.1
  5. def's return value! > def foo; end! => :foo! >

    def foo; end! => nil! < Ruby 2.1 Ruby 2.1
  6. def's return value! module Foo! def public_method! end! ! private

    # <- this sucks! def a_private_method! end! end!
  7. def's return value! 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]!
  8. def's return value! 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]!
  9. Using a Refinement! module Permalinker! refine String do! def permalinkify!

    downcase.split.join("-")! end! end! end! ! class Post! ->using Permalinker! ! def initialize(title)! @title = title! end! ! def permalink! @title.permalinkify! end! end!
  10. Using a Refinement! module Permalinker! refine String do! def permalinkify!

    downcase.split.join("-")! end! end! end! ! class Post! using Permalinker! ! def initialize(title)! @title = title! end! ! def permalink! ->@title.permalinkify! end! end!
  11. Required Keyword Args! > permalinkify(delimiter: "-lol-")! ArgumentError: missing keyword: str!

    from (irb):49! from /usr/local/var/rbenv/ versions/2.1.0/bin/irb:11:in `<main>'!
  12. Generational GC! •  split objects into young and old based

    on whether they survive a garbage collection run.! •  concentrate on freeing up memory on the young generation.!
  13. Why "Restricted"?! •  still using Mark and Sweep to garbage

    collect young/ old generations! •  preserve compatibility with C extensions!
  14. require 'objspace'! ! class Post! def initialize(title)! @title = title!

    end! ! def tags! %w(ruby programming code).map do |tag|! tag.upcase! end! end! end!
  15. ObjectSpace.trace_object_allocations_start! a = Post.new("title")! b = a.tags! ObjectSpace.trace_object_allocations_stop! ! !

    ObjectSpace.allocation_sourcefile(b) # post.rb! ObjectSpace.allocation_sourceline(b) # ObjectSpace.allocation_class_path(b) # Array! ObjectSpace.allocation_method_id(b) # map! Object Allocation Tracing!
  16. require 'allocation_stats'! ! class Post! def initialize(title)! @title = title!

    end! ! def tags! %w(ruby programming code).map do |tag|! tag.upcase! end! end! end! ! stats = AllocationStats.trace do! post = Post.new("title")! post.tags! end! ! puts stats.allocations(alias_paths: true).to_text!
  17. sourcefile sourceline class_path method_id memsize class! ---------- ---------- ---------- ---------

    ------- ------! post.rb 10 String upcase 0 String! post.rb 10 String upcase 0 String! post.rb 10 String upcase 0 String! post.rb 9 Array map 0 Array! post.rb 9 Post tags 0 Array! post.rb 9 Post tags 0 String! post.rb 9 Post tags 0 String! post.rb 9 Post tags 0 String! post.rb 17 Class new 0 Post! post.rb 17 0 String! Object Allocation Tracing!
  18. What's New?! 1.  Rational Number & Complex ! Number Literals

    ! 2.  def‘s return value! 3.  Refinements! 4.  Required Keyword Arguments! 5.  Garbage Collector! 6.  Object Allocation Tracing!