SlideShare a Scribd company logo
1 of 56
Download to read offline
Ruby 2.1
Benjamin Tan Wei Hao (@bentanweihao)!
What's New?!
1. 
!
2. 
3. 
4. 
5. 
6. 

Rational Number & Complex
Number Literals !
def‘s return value!
Refinements!
Required Keyword Arguments!
Garbage Collector!
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. 
!
2. 
3. 
4. 
5. 
6. 

Rational Number & Complex
Number Literals !
def‘s return value!
Refinements!
Required Keyword Arguments!
Garbage Collector!
Object Allocation Tracing!
Complex/
Rational
Literals
Complex Literals!
< Ruby 2.1
> Complex(2, 3)!
=> (2+3i)!
Complex Literals!
< Ruby 2.1
> Complex(2, 3)!
=> (2+3i)!

Ruby 2.1
> (2 + 3i)!
=> (2+3i)!
> (2 + 3i) + Complex(5, 4i)!
=> (3+3i)!
Rational Literals!
> 2/3.0 + 5/4.0!
=> 1.91666666666665!

< Ruby 2.1
Rational Literals!
> 2/3.0 + 5/4.0!
=> 1.91666666666665!

< Ruby 2.1

Ruby 2.1
> 2/3r + 5/4r!
=> (23/12)!
def's
return
value
def's return value!
< Ruby 2.1
> def foo; end!
=> nil!
def's return value!
< Ruby 2.1
> def foo; end!
=> nil!

Ruby 2.1
> def foo; end!
=> :foo!
def's return value!
module Foo!
def public_method!
end!
!
private # <- this sucks!
def a_private_method!
end!
end!
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!

class Post!
->using Permalinker!
module Permalinker!
!
refine String do!
def permalinkify!
def initialize(title)!
downcase.split.join("-")!
@title = title!
end!
end!
end!
end!
!
!
def permalink!
@title.permalinkify!
end!
end!
Using a Refinement!

class Post!
using Permalinker!
module Permalinker!
!
refine String do!
def permalinkify!
def initialize(title)!
downcase.split.join("-")!
@title = title!
end!
end!
end!
end!
!
!
def permalink!
->@title.permalinkify!
end!
end!
Required
Keyword ArGS
Required Keyword Args!
< Ruby 2.1
def permalinkfiy(str, delimiter: "-")!
str.downcase.split.join(delimiter)!
end!

Question: How do we make str
required?!
Required Keyword Args!
Ruby 2.1
def permalinkfiy(str:, delimiter: "-")!
str.downcase.split.join(delimiter)!
end!
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!
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!
Object Allocation Tracing!
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!
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!
Object Allocation Tracing!
sourcefile
---------post.rb
post.rb
post.rb
post.rb
post.rb
post.rb
post.rb
post.rb
post.rb
post.rb

sourceline
---------10
10
10
9
9
9
9
9
17
17

class_path
---------String
String
String
Array
Post
Post
Post
Post
Class

method_id
--------upcase
upcase
upcase
map
tags
tags
tags
tags
new

memsize
------0
0
0
0
0
0
0
0
0
0

class!
------!
String!
String!
String!
Array!
Array!
String!
String!
String!
Post!
String!
gem install
allocation_stats!
https://github.com/srawlins/
allocation_stats!
What's New?!
1. 
!
2. 
3. 
4. 
5. 
6. 

Rational Number & Complex
Number Literals !
def‘s return value!
Refinements!
Required Keyword Arguments!
Garbage Collector!
Object Allocation Tracing!
USE Ruby 2.1!
FOllow me on
twitter!

@bentanweihao!
Learn to build your own concurrent, distributed
web application – The fun & easy way!
http://www.exotpbook.com/!
Thanks! <3



@bentanweihao

benjamintanweihao.github.io!

More Related Content

Similar to Ruby 2.1

RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!Gautam Rege
 
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar Batsov
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar BatsovRuby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar Batsov
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar BatsovMichael Kimathi
 
Решардинг Redis "на живую" (Андрей Смирнов, Василий Евсеенко)
Решардинг Redis "на живую" (Андрей Смирнов, Василий Евсеенко)Решардинг Redis "на живую" (Андрей Смирнов, Василий Евсеенко)
Решардинг Redis "на живую" (Андрей Смирнов, Василий Евсеенко)Ontico
 
Aprendendo solid com exemplos
Aprendendo solid com exemplosAprendendo solid com exemplos
Aprendendo solid com exemplosvinibaggio
 
TypeProf for IDE: Enrich Development Experience without Annotations
TypeProf for IDE: Enrich Development Experience without AnnotationsTypeProf for IDE: Enrich Development Experience without Annotations
TypeProf for IDE: Enrich Development Experience without Annotationsmametter
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Wen-Tien Chang
 
2016-05-12 DCRUG React.rb
2016-05-12 DCRUG React.rb2016-05-12 DCRUG React.rb
2016-05-12 DCRUG React.rbawwaiid
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technologyppparthpatel123
 
RSpec 3.0: Under the Covers
RSpec 3.0: Under the CoversRSpec 3.0: Under the Covers
RSpec 3.0: Under the CoversBrian Gesiak
 
Migrating To Ruby1.9
Migrating To Ruby1.9Migrating To Ruby1.9
Migrating To Ruby1.9tomaspavelka
 
A rough guide to JavaScript Performance
A rough guide to JavaScript PerformanceA rough guide to JavaScript Performance
A rough guide to JavaScript Performanceallmarkedup
 
Rails for PHP Developers
Rails for PHP DevelopersRails for PHP Developers
Rails for PHP DevelopersRobert Dempsey
 
Embedding Languages Without Breaking Tools
Embedding Languages Without Breaking ToolsEmbedding Languages Without Breaking Tools
Embedding Languages Without Breaking ToolsLukas Renggli
 
Password Storage And Attacking In PHP - PHP Argentina
Password Storage And Attacking In PHP - PHP ArgentinaPassword Storage And Attacking In PHP - PHP Argentina
Password Storage And Attacking In PHP - PHP ArgentinaAnthony Ferrara
 
Gemification for Ruby 2.5/3.0
Gemification for Ruby 2.5/3.0Gemification for Ruby 2.5/3.0
Gemification for Ruby 2.5/3.0Hiroshi SHIBATA
 

Similar to Ruby 2.1 (20)

RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!
 
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar Batsov
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar BatsovRuby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar Batsov
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar Batsov
 
Week1
Week1Week1
Week1
 
Week2
Week2Week2
Week2
 
How DSL works on Ruby
How DSL works on RubyHow DSL works on Ruby
How DSL works on Ruby
 
Rails by example
Rails by exampleRails by example
Rails by example
 
Решардинг Redis "на живую" (Андрей Смирнов, Василий Евсеенко)
Решардинг Redis "на живую" (Андрей Смирнов, Василий Евсеенко)Решардинг Redis "на живую" (Андрей Смирнов, Василий Евсеенко)
Решардинг Redis "на живую" (Андрей Смирнов, Василий Евсеенко)
 
Aprendendo solid com exemplos
Aprendendo solid com exemplosAprendendo solid com exemplos
Aprendendo solid com exemplos
 
TypeProf for IDE: Enrich Development Experience without Annotations
TypeProf for IDE: Enrich Development Experience without AnnotationsTypeProf for IDE: Enrich Development Experience without Annotations
TypeProf for IDE: Enrich Development Experience without Annotations
 
Php resque
Php resquePhp resque
Php resque
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
 
2016-05-12 DCRUG React.rb
2016-05-12 DCRUG React.rb2016-05-12 DCRUG React.rb
2016-05-12 DCRUG React.rb
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
 
RSpec 3.0: Under the Covers
RSpec 3.0: Under the CoversRSpec 3.0: Under the Covers
RSpec 3.0: Under the Covers
 
Migrating To Ruby1.9
Migrating To Ruby1.9Migrating To Ruby1.9
Migrating To Ruby1.9
 
A rough guide to JavaScript Performance
A rough guide to JavaScript PerformanceA rough guide to JavaScript Performance
A rough guide to JavaScript Performance
 
Rails for PHP Developers
Rails for PHP DevelopersRails for PHP Developers
Rails for PHP Developers
 
Embedding Languages Without Breaking Tools
Embedding Languages Without Breaking ToolsEmbedding Languages Without Breaking Tools
Embedding Languages Without Breaking Tools
 
Password Storage And Attacking In PHP - PHP Argentina
Password Storage And Attacking In PHP - PHP ArgentinaPassword Storage And Attacking In PHP - PHP Argentina
Password Storage And Attacking In PHP - PHP Argentina
 
Gemification for Ruby 2.5/3.0
Gemification for Ruby 2.5/3.0Gemification for Ruby 2.5/3.0
Gemification for Ruby 2.5/3.0
 

Recently uploaded

Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 

Recently uploaded (20)

Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 

Ruby 2.1