Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ruby 2.1
Search
Benjamin Tan Wei Hao
January 21, 2014
Programming
4
280
Ruby 2.1
Gave this talk during January 2014 Singapore Ruby Brigade meetup.
Benjamin Tan Wei Hao
January 21, 2014
Tweet
Share
More Decks by Benjamin Tan Wei Hao
See All by Benjamin Tan Wei Hao
Implementing a Worker Pool in 4 Acts
benjamintan
1
250
Rubyists! Have a sip of Elixir!
benjamintan
23
1.7k
Ruby + Elixir: Polyglottin' FTW!
benjamintan
14
2k
Elixir - Peeking into Processes, OTP & Supervisors
benjamintan
13
940
Hello, Elixir!
benjamintan
10
840
Code Rippa
benjamintan
3
240
Other Decks in Programming
See All in Programming
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
9
3.3k
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
200
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
Outline View in SwiftUI
1024jp
1
330
C++でシェーダを書く
fadis
6
4.1k
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
980
Ethereum_.pdf
nekomatu
0
470
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
3
690
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
990
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.5k
Jakarta EE meets AI
ivargrimstad
0
150
Featured
See All Featured
Fireside Chat
paigeccino
34
3k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
BBQ
matthewcrist
85
9.3k
Producing Creativity
orderedlist
PRO
341
39k
Building Your Own Lightsaber
phodgson
103
6.1k
4 Signs Your Business is Dying
shpigford
180
21k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Writing Fast Ruby
sferik
627
61k
It's Worth the Effort
3n
183
27k
Docker and Python
trallard
40
3.1k
Scaling GitHub
holman
458
140k
Transcript
Ruby 2.1 Benjamin Tan Wei Hao (@bentanweihao)!
None
None
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!
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. Rational Number & Complex ! Number Literals
! 2. def‘s return value! 3. Refinements! 4. Required Keyword Arguments! 5. Garbage Collector! 6. Object Allocation Tracing!
Complex/ Rational Literals
Complex Literals! > Complex(2, 3)! => (2+3i)! < Ruby 2.1
Complex Literals! > (2 + 3i)! => (2+3i)! > (2
+ 3i) + Complex(5, 4i)! => (3+3i)! > Complex(2, 3)! => (2+3i)! < Ruby 2.1 Ruby 2.1
Rational Literals! > 2/3.0 + 5/4.0! => 1.91666666666665! < Ruby
2.1
Rational Literals! > 2/3r + 5/4r! => (23/12)! > 2/3.0
+ 5/4.0! => 1.91666666666665! < Ruby 2.1 Ruby 2.1
def's return value
def's return value! > def foo; end! => nil! <
Ruby 2.1
def's return value! > def foo; end! => :foo! >
def foo; end! => nil! < Ruby 2.1 Ruby 2.1
def's return value! module Foo! def public_method! end! ! private
# <- this sucks! def a_private_method! end! end!
None
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! 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!
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!
Required Keyword ArGS
Required Keyword Args! def permalinkfiy(str, delimiter: "-")! str.downcase.split.join(delimiter)! end! <
Ruby 2.1 Question: How do we make str required?!
Required Keyword Args! def permalinkfiy(str:, delimiter: "-")! str.downcase.split.join(delimiter)! end! Ruby
2.1
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!
None
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!
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!
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!
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!
None
gem install allocation_stats! https://github.com/srawlins/ allocation_stats!
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!
USE Ruby 2.1!
FOllow me on twitter! @bentanweihao!
http://www.exotpbook.com/! Learn to build your own concurrent, distributed web application
– The fun & easy way!
Thanks! <3 @bentanweihao benjamintanweihao.github.io!