• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Ruby 2.1
 

Ruby 2.1

on

  • 311 views

 

Statistics

Views

Total Views
311
Views on SlideShare
311
Embed Views
0

Actions

Likes
1
Downloads
1
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Ruby 2.1 Ruby 2.1 Presentation Transcript

    • Ruby 2.1 Benjamin Tan Wei Hao (@bentanweihao)!
    • 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!
    • BUT FIRST!
    • Getting Ruby 2.1
    • RVM! $ rvm get head! $ rvm install ruby-2.1.0! $ rvm use ruby-2.1.0!
    • RBENV! $ rbenv install 2.1.0! $ rbenv rehash! $ rbenv global 2.1.0!
    • what's new?
    • 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!
    • Complex/ Rational Literals
    • Complex Literals! < Ruby 2.1 > Complex(2, 3)! => (2+3i)!
    • Complex Literals! < Ruby 2.1 > Complex(2, 3)! => (2+3i)! Ruby 2.1 > (2 + 3i)! => (2+3i)! > (2 + 3i) + Complex(5, 4i)! => (3+3i)!
    • Rational Literals! > 2/3.0 + 5/4.0! => 1.91666666666665! < Ruby 2.1
    • Rational Literals! > 2/3.0 + 5/4.0! => 1.91666666666665! < Ruby 2.1 Ruby 2.1 > 2/3r + 5/4r! => (23/12)!
    • def's return value
    • def's return value! < Ruby 2.1 > def foo; end! => nil!
    • def's return value! < Ruby 2.1 > def foo; end! => nil! Ruby 2.1 > def foo; end! => :foo!
    • def's return value! module Foo! def public_method! end! ! private # <- this sucks! def a_private_method! end! end!
    • 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]!
    • 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]!
    • Refinements
    • Refinements are no longer experimental.!
    • Refinements! class String! def count! Float::INFINITY! end! end!
    • Refinements let's us scope our modifications!
    • Defining a Refinement! module Permalinker! refine String do! def permalinkify! downcase.split.join("-")! end! end! end! !
    • 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!
    • 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!
    • Required Keyword ArGS
    • Required Keyword Args! < Ruby 2.1 def permalinkfiy(str, delimiter: "-")! str.downcase.split.join(delimiter)! end! Question: How do we make str required?!
    • Required Keyword Args! Ruby 2.1 def permalinkfiy(str:, delimiter: "-")! str.downcase.split.join(delimiter)! end!
    • 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>'!
    • RGengc Restricted Generational Garbage Collector!
    • Ruby 1.8: Simple M&S! Credits: http://tmm1.net/ruby21-rgengc/!
    • Ruby 1.9.3: Lazy Sweep! Credits: http://tmm1.net/ruby21-rgengc/!
    • Ruby 2.0: Bitmap for COW-Safety! Credits: http://tmm1.net/ruby21-rgengc/!
    • Ruby 2.1: RGenGC! Credits: http://tmm1.net/ruby21-rgengc/!
    • Generational GC! Key Idea:! ! Objects that are most recently created often die young.!
    • 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.!
    • Why "Restricted"?! •  still using Mark and Sweep to garbage collect young/ old generations! •  preserve compatibility with C extensions!
    • Ojbect Allocation Tracing
    • require 'objspace'!  ! class Post!   def initialize(title)!     @title = title!   end!  !   def tags!     %w(ruby programming code).map do |tag|!       tag.upcase!     end!   end! end!
    • 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!
    • Ojbect Allocation Tracing gives only raw data.!
    • gem install allocation_stats! https://github.com/srawlins/ allocation_stats!
    • 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!
    • 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!
    • gem install allocation_stats! https://github.com/srawlins/ allocation_stats!
    • 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!
    • USE Ruby 2.1!
    • FOllow me on twitter! @bentanweihao!
    • Learn to build your own concurrent, distributed web application – The fun & easy way! http://www.exotpbook.com/!
    • Thanks! <3 
 @bentanweihao
 benjamintanweihao.github.io!