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
Jakarta EE meets AI
ivargrimstad
0
540
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
920
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
330
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
100
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
Jakarta EE meets AI
ivargrimstad
0
150
Better Code Design in PHP
afilina
PRO
0
120
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
Featured
See All Featured
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
A Tale of Four Properties
chriscoyier
156
23k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Adopting Sorbet at Scale
ufuk
73
9.1k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
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!