SlideShare uma empresa Scribd logo
1 de 56
Baixar para ler 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!

Mais conteúdo relacionado

Semelhante a Ruby 2.1

Решардинг Redis "на живую" (Андрей Смирнов, Василий Евсеенко)
Решардинг Redis "на живую" (Андрей Смирнов, Василий Евсеенко)Решардинг Redis "на живую" (Андрей Смирнов, Василий Евсеенко)
Решардинг Redis "на живую" (Андрей Смирнов, Василий Евсеенко)
Ontico
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
Wen-Tien Chang
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
ppparthpatel123
 
Migrating To Ruby1.9
Migrating To Ruby1.9Migrating To Ruby1.9
Migrating To Ruby1.9
tomaspavelka
 

Semelhante a 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
 

Último

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 

Ruby 2.1