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.

F3fe8fa67ae6dbc1d19189e80c9e96a2?s=128

Benjamin Tan Wei Hao

January 21, 2014
Tweet

Transcript

  1. Ruby 2.1 Benjamin Tan Wei Hao (@bentanweihao)!

  2. None
  3. None
  4. 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!
  5. BUT FIRST!

  6. Getting Ruby 2.1

  7. RVM! $ rvm get head! $ rvm install ruby-2.1.0! $

    rvm use ruby-2.1.0!
  8. RBENV! $ rbenv install 2.1.0! $ rbenv rehash! $ rbenv

    global 2.1.0!
  9. what's new?

  10. 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!
  11. Complex/ Rational Literals

  12. Complex Literals! > Complex(2, 3)! => (2+3i)! < Ruby 2.1

  13. Complex Literals! > (2 + 3i)! => (2+3i)! > (2

    + 3i) + Complex(5, 4i)! => (3+3i)! > Complex(2, 3)! => (2+3i)! < Ruby 2.1 Ruby 2.1
  14. Rational Literals! > 2/3.0 + 5/4.0! => 1.91666666666665! < Ruby

    2.1
  15. Rational Literals! > 2/3r + 5/4r! => (23/12)! > 2/3.0

    + 5/4.0! => 1.91666666666665! < Ruby 2.1 Ruby 2.1
  16. def's return value

  17. def's return value! > def foo; end! => nil! <

    Ruby 2.1
  18. def's return value! > def foo; end! => :foo! >

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

    # <- this sucks! def a_private_method! end! end!
  20. None
  21. 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]!
  22. 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]!
  23. Refinements

  24. Refinements are no longer experimental.!

  25. Refinements! class String! def count! Float::INFINITY! end! end!

  26. Refinements let's us scope our modifications!

  27. Defining a Refinement! module Permalinker! refine String do! def permalinkify!

    downcase.split.join("-")! end! end! end! !
  28. 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!
  29. 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!
  30. Required Keyword ArGS

  31. Required Keyword Args! def permalinkfiy(str, delimiter: "-")! str.downcase.split.join(delimiter)! end! <

    Ruby 2.1 Question: How do we make str required?!
  32. Required Keyword Args! def permalinkfiy(str:, delimiter: "-")! str.downcase.split.join(delimiter)! end! Ruby

    2.1
  33. 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>'!
  34. RGengc Restricted Generational Garbage Collector!

  35. Ruby 1.8: Simple M&S! Credits: http://tmm1.net/ruby21-rgengc/!

  36. Ruby 1.9.3: Lazy Sweep! Credits: http://tmm1.net/ruby21-rgengc/!

  37. Ruby 2.0: Bitmap for COW-Safety! Credits: http://tmm1.net/ruby21-rgengc/!

  38. Ruby 2.1: RGenGC! Credits: http://tmm1.net/ruby21-rgengc/!

  39. Generational GC! Key Idea:! ! Objects that are most recently

    created often die young.!
  40. 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.!
  41. Why "Restricted"?! •  still using Mark and Sweep to garbage

    collect young/ old generations! •  preserve compatibility with C extensions!
  42. None
  43. Ojbect Allocation Tracing

  44. require 'objspace'! ! class Post! def initialize(title)! @title = title!

    end! ! def tags! %w(ruby programming code).map do |tag|! tag.upcase! end! end! end!
  45. 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!
  46. Ojbect Allocation Tracing gives only raw data.!

  47. gem install allocation_stats! https://github.com/srawlins/ allocation_stats!

  48. 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!
  49. 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!
  50. None
  51. gem install allocation_stats! https://github.com/srawlins/ allocation_stats!

  52. 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!
  53. USE Ruby 2.1!

  54. FOllow me on twitter! @bentanweihao!

  55. http://www.exotpbook.com/! Learn to build your own concurrent, distributed web application

    – The fun & easy way!
  56. Thanks! <3
 @bentanweihao
 benjamintanweihao.github.io!