Ruby 2.1

713 views

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
713
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Ruby 2.1

  1. 1. Ruby 2.1 Benjamin Tan Wei Hao (@bentanweihao)!
  2. 2. What's New?! 1.  ! 2.  3.  4.  5.  6.  Rational Number & Complex Number Literals ! def‘s return value! Refinements! Required Keyword Arguments! Garbage Collector! Object Allocation Tracing!
  3. 3. BUT FIRST!
  4. 4. Getting Ruby 2.1
  5. 5. RVM! $ rvm get head! $ rvm install ruby-2.1.0! $ rvm use ruby-2.1.0!
  6. 6. RBENV! $ rbenv install 2.1.0! $ rbenv rehash! $ rbenv global 2.1.0!
  7. 7. what's new?
  8. 8. What's New?! 1.  ! 2.  3.  4.  5.  6.  Rational Number & Complex Number Literals ! def‘s return value! Refinements! Required Keyword Arguments! Garbage Collector! Object Allocation Tracing!
  9. 9. Complex/ Rational Literals
  10. 10. Complex Literals! < Ruby 2.1 > Complex(2, 3)! => (2+3i)!
  11. 11. Complex Literals! < Ruby 2.1 > Complex(2, 3)! => (2+3i)! Ruby 2.1 > (2 + 3i)! => (2+3i)! > (2 + 3i) + Complex(5, 4i)! => (3+3i)!
  12. 12. Rational Literals! > 2/3.0 + 5/4.0! => 1.91666666666665! < Ruby 2.1
  13. 13. Rational Literals! > 2/3.0 + 5/4.0! => 1.91666666666665! < Ruby 2.1 Ruby 2.1 > 2/3r + 5/4r! => (23/12)!
  14. 14. def's return value
  15. 15. def's return value! < Ruby 2.1 > def foo; end! => nil!
  16. 16. def's return value! < Ruby 2.1 > def foo; end! => nil! Ruby 2.1 > def foo; end! => :foo!
  17. 17. def's return value! module Foo! def public_method! end! ! private # <- this sucks! def a_private_method! end! end!
  18. 18. 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]!
  19. 19. 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]!
  20. 20. Refinements
  21. 21. Refinements are no longer experimental.!
  22. 22. Refinements! class String! def count! Float::INFINITY! end! end!
  23. 23. Refinements let's us scope our modifications!
  24. 24. Defining a Refinement! module Permalinker! refine String do! def permalinkify! downcase.split.join("-")! end! end! end! !
  25. 25. Using a Refinement! class Post! ->using Permalinker! module Permalinker! ! refine String do! def permalinkify! def initialize(title)! downcase.split.join("-")! @title = title! end! end! end! end! ! ! def permalink! @title.permalinkify! end! end!
  26. 26. Using a Refinement! class Post! using Permalinker! module Permalinker! ! refine String do! def permalinkify! def initialize(title)! downcase.split.join("-")! @title = title! end! end! end! end! ! ! def permalink! ->@title.permalinkify! end! end!
  27. 27. Required Keyword ArGS
  28. 28. Required Keyword Args! < Ruby 2.1 def permalinkfiy(str, delimiter: "-")! str.downcase.split.join(delimiter)! end! Question: How do we make str required?!
  29. 29. Required Keyword Args! Ruby 2.1 def permalinkfiy(str:, delimiter: "-")! str.downcase.split.join(delimiter)! end!
  30. 30. 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>'!
  31. 31. RGengc Restricted Generational Garbage Collector!
  32. 32. Ruby 1.8: Simple M&S! Credits: http://tmm1.net/ruby21-rgengc/!
  33. 33. Ruby 1.9.3: Lazy Sweep! Credits: http://tmm1.net/ruby21-rgengc/!
  34. 34. Ruby 2.0: Bitmap for COW-Safety! Credits: http://tmm1.net/ruby21-rgengc/!
  35. 35. Ruby 2.1: RGenGC! Credits: http://tmm1.net/ruby21-rgengc/!
  36. 36. Generational GC! Key Idea:! ! Objects that are most recently created often die young.!
  37. 37. 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.!
  38. 38. Why "Restricted"?! •  still using Mark and Sweep to garbage collect young/ old generations! •  preserve compatibility with C extensions!
  39. 39. Ojbect Allocation Tracing
  40. 40. require 'objspace'!  ! class Post!   def initialize(title)!     @title = title!   end!  !   def tags!     %w(ruby programming code).map do |tag|!       tag.upcase!     end!   end! end!
  41. 41. Object Allocation Tracing! 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!
  42. 42. Ojbect Allocation Tracing gives only raw data.!
  43. 43. gem install allocation_stats! https://github.com/srawlins/ allocation_stats!
  44. 44. 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!
  45. 45. Object Allocation Tracing! sourcefile ---------post.rb post.rb post.rb post.rb post.rb post.rb post.rb post.rb post.rb post.rb sourceline ---------10 10 10 9 9 9 9 9 17 17 class_path ---------String String String Array Post Post Post Post Class method_id --------upcase upcase upcase map tags tags tags tags new memsize ------0 0 0 0 0 0 0 0 0 0 class! ------! String! String! String! Array! Array! String! String! String! Post! String!
  46. 46. gem install allocation_stats! https://github.com/srawlins/ allocation_stats!
  47. 47. What's New?! 1.  ! 2.  3.  4.  5.  6.  Rational Number & Complex Number Literals ! def‘s return value! Refinements! Required Keyword Arguments! Garbage Collector! Object Allocation Tracing!
  48. 48. USE Ruby 2.1!
  49. 49. FOllow me on twitter! @bentanweihao!
  50. 50. Learn to build your own concurrent, distributed web application – The fun & easy way! http://www.exotpbook.com/!
  51. 51. Thanks! <3 
 @bentanweihao
 benjamintanweihao.github.io!

×