Slide 1

Slide 1 text

Ruby 2.1 Benjamin Tan Wei Hao (@bentanweihao)!

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

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!

Slide 5

Slide 5 text

BUT FIRST!

Slide 6

Slide 6 text

Getting Ruby 2.1

Slide 7

Slide 7 text

RVM! $ rvm get head! $ rvm install ruby-2.1.0! $ rvm use ruby-2.1.0!

Slide 8

Slide 8 text

RBENV! $ rbenv install 2.1.0! $ rbenv rehash! $ rbenv global 2.1.0!

Slide 9

Slide 9 text

what's new?

Slide 10

Slide 10 text

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!

Slide 11

Slide 11 text

Complex/ Rational Literals

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Complex Literals! > (2 + 3i)! => (2+3i)! > (2 + 3i) + Complex(5, 4i)! => (3+3i)! > Complex(2, 3)! => (2+3i)! < Ruby 2.1 Ruby 2.1

Slide 14

Slide 14 text

Rational Literals! > 2/3.0 + 5/4.0! => 1.91666666666665! < Ruby 2.1

Slide 15

Slide 15 text

Rational Literals! > 2/3r + 5/4r! => (23/12)! > 2/3.0 + 5/4.0! => 1.91666666666665! < Ruby 2.1 Ruby 2.1

Slide 16

Slide 16 text

def's return value

Slide 17

Slide 17 text

def's return value! > def foo; end! => nil! < Ruby 2.1

Slide 18

Slide 18 text

def's return value! > def foo; end! => :foo! > def foo; end! => nil! < Ruby 2.1 Ruby 2.1

Slide 19

Slide 19 text

def's return value! module Foo! def public_method! end! ! private # <- this sucks! def a_private_method! end! end!

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

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]!

Slide 22

Slide 22 text

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]!

Slide 23

Slide 23 text

Refinements

Slide 24

Slide 24 text

Refinements are no longer experimental.!

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Refinements let's us scope our modifications!

Slide 27

Slide 27 text

Defining a Refinement! module Permalinker! refine String do! def permalinkify! downcase.split.join("-")! end! end! end! !

Slide 28

Slide 28 text

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!

Slide 29

Slide 29 text

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!

Slide 30

Slide 30 text

Required Keyword ArGS

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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 `'!

Slide 34

Slide 34 text

RGengc Restricted Generational Garbage Collector!

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Generational GC! Key Idea:! ! Objects that are most recently created often die young.!

Slide 40

Slide 40 text

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.!

Slide 41

Slide 41 text

Why "Restricted"?! •  still using Mark and Sweep to garbage collect young/ old generations! •  preserve compatibility with C extensions!

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

Ojbect Allocation Tracing

Slide 44

Slide 44 text

require 'objspace'! ! class Post! def initialize(title)! @title = title! end! ! def tags! %w(ruby programming code).map do |tag|! tag.upcase! end! end! end!

Slide 45

Slide 45 text

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!

Slide 46

Slide 46 text

Ojbect Allocation Tracing gives only raw data.!

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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!

Slide 49

Slide 49 text

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!

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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!

Slide 53

Slide 53 text

USE Ruby 2.1!

Slide 54

Slide 54 text

FOllow me on twitter! @bentanweihao!

Slide 55

Slide 55 text

http://www.exotpbook.com/! Learn to build your own concurrent, distributed web application – The fun & easy way!

Slide 56

Slide 56 text

Thanks! <3
 @bentanweihao
 benjamintanweihao.github.io!