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
950
Hello, Elixir!
benjamintan
10
840
Code Rippa
benjamintan
3
240
Other Decks in Programming
See All in Programming
42 best practices for Symfony, a decade later
tucksaun
1
180
Mermaid x AST x 生成AI = コードとドキュメントの完全同期への道
shibuyamizuho
0
160
talk-with-local-llm-with-web-streams-api
kbaba1001
0
180
ドメインイベント増えすぎ問題
h0r15h0
1
230
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
130
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
210
プロダクトの品質に コミットする / Commit to Product Quality
pekepek
2
770
Security_for_introducing_eBPF
kentatada
0
110
今年一番支援させていただいたのは認証系サービスでした
satoshi256kbyte
1
250
From Translations to Multi Dimension Entities
alexanderschranz
2
130
useSyncExternalStoreを使いまくる
ssssota
6
1k
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
310
Featured
See All Featured
The Pragmatic Product Professional
lauravandoore
32
6.3k
The Invisible Side of Design
smashingmag
298
50k
Building Your Own Lightsaber
phodgson
103
6.1k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
KATA
mclloyd
29
14k
Making the Leap to Tech Lead
cromwellryan
133
9k
Being A Developer After 40
akosma
87
590k
Visualization
eitanlees
146
15k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Git: the NoSQL Database
bkeepers
PRO
427
64k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
Facilitating Awesome Meetings
lara
50
6.1k
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!