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
Golang と Erlang
taiyow
8
1.9k
Vue3の一歩踏み込んだパフォーマンスチューニング2024
hal_spidernight
3
3.1k
約9000個の自動テストの 時間を50分->10分に短縮 Flakyテストを1%以下に抑えた話
hatsu38
23
11k
WEBエンジニア向けAI活用入門
sutetotanuki
0
300
推し活の ハイトラフィックに立ち向かう Railsとアーキテクチャ - Kaigi on Rails 2024
falcon8823
6
2.2k
推し活としてのrails new/oshikatsu_ha_iizo
sakahukamaki
3
1.7k
Progressive Web Apps für Desktop und Mobile mit Angular (Hands-on)
christianliebel
PRO
0
110
ECSのサービス間通信 4つの方法を比較する 〜Canary,Blue/Greenも添えて〜
tkikuc
11
2.3k
Vue SFCのtemplateでTypeScriptの型を活用しよう
tsukkee
3
1.5k
2万ページのSSG運用における工夫と注意点 / Vue Fes Japan 2024
chinen
3
1.3k
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
430
gopls を改造したら開発生産性が高まった
satorunooshie
8
240
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1030
460k
How to Ace a Technical Interview
jacobian
275
23k
[RailsConf 2023] Rails as a piece of cake
palkan
51
4.9k
The Language of Interfaces
destraynor
154
24k
Done Done
chrislema
181
16k
KATA
mclloyd
29
13k
The Invisible Side of Design
smashingmag
297
50k
We Have a Design System, Now What?
morganepeng
50
7.2k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Facilitating Awesome Meetings
lara
49
6k
Art, The Web, and Tiny UX
lynnandtonic
296
20k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.2k
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!