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
830
Code Rippa
benjamintan
3
240
Other Decks in Programming
See All in Programming
現場で役立つモデリング 超入門
masuda220
PRO
13
2.9k
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
550
Kaigi on Rails 2024 - Rails APIモードのためのシンプルで効果的なCSRF対策 / kaigionrails-2024-csrf
corocn
5
3.4k
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
7
2.8k
Go言語でターミナルフレンドリーなAIコマンド、afaを作った/fukuokago20_afa
monochromegane
2
140
gopls を改造したら開発生産性が高まった
satorunooshie
8
240
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
9
1k
Piniaの現状と今後
waka292
5
1.5k
Identifying User Idenity
moro
6
7.9k
Progressive Web Apps für Desktop und Mobile mit Angular (Hands-on)
christianliebel
PRO
0
110
cXML という電子商取引の トランザクションを支える プロトコルと向きあっている話
phigasui
3
2.3k
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
850
Featured
See All Featured
A designer walks into a library…
pauljervisheath
202
24k
Making the Leap to Tech Lead
cromwellryan
132
8.9k
For a Future-Friendly Web
brad_frost
175
9.4k
It's Worth the Effort
3n
183
27k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Side Projects
sachag
452
42k
RailsConf 2023
tenderlove
29
880
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
Statistics for Hackers
jakevdp
796
220k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
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!