SlideShare une entreprise Scribd logo
1  sur  35
Télécharger pour lire hors ligne
Migrating to
Ruby 1.9



               Bruce Williams
Bruce Williams
Perpetrator of much random Ruby hackery, language tourist


Rubyist since 2001               (Full-time since 2005)

Open source developer, contributer, technical editor, designer
Occasionally blogs at http://codefluency.com
Ruby 1.8                  Ruby 1.9
Stable.                   Unstable, transitional.
The syntax and language   Many new syntax and
features you know and     language features.
probably love.
                          Better performance,
The performance profile    especially for computationally
you know and might hate   intensive operations.
a little.
1.9 is a hint.
1.9 is a hint.
   Get ready for 2.0.
Ruby’s Releases
From Toybox to Toolshed
                                             (dev)
(dev)                                         1.7                                                         (dev)
           1.6.1 1.6.3 1.6.5                         1.8.1                  1.8.4
 1.5                                                                                                       1.9
        1.6.0 1.6.2 1.6.4    1.6.7   1.6.8       1.8.0       1.8.2                    1.8.5     1.8.6
                                                                        1.8.3



 ‘00           ‘01         ‘02          ‘03           ‘04      ‘05              ‘06           ‘07         ‘08


 Japan                   Beyond Japan                                “... on Rails”                     Expansion
Standard Library
    rubygems (+ prelude & ruby --disable-gems), rake, json (pure,
+   ext), ripper, probeprofiler, securerandom, HMAC
    digests
~   csv replaced by FasterCSV implementation

-   soap, wsdl, base64, some rarely used, old libraries
Parser Changes
Flexibility and Obscurity (for the moment)
Parser Changes
New Hash Literal
{a: quot;fooquot;}
# => {:a=>quot;fooquot;}

{a: quot;barquot;, :b => quot;bazquot;}
# => {:a=>quot;barquot;, :b=>quot;bazquot;}
Parser Changes
New Proc Literal
multiply_by_2 = ->(x) { x * 2 }
# => #<Proc:0x3c5a50>

multiply_by_2.(4)
# => 8
Parser Changes
Splat more flexibly

names = %w(joe john bill)
[*names, 'jack']
# => [quot;joequot;, quot;johnquot;, quot;billquot;, quot;jackquot;]
Parser Changes
Method parameter ordering

def say(language=:english, text)
  puts Translator[language].translate(text)
end
say quot;helloquot;
# hello
say :spanish, quot;helloquot;
# hola
Migration Risk Factors

Text processing
“Clever” assignment with blocks
Some Hash enumerations
Metaprogramming, code generation
Tests are Good
I was surprised at how much work my 11th hour integration of the
FasterCSV code was. It was a pure Ruby library that really didn't do
a lot of fancy tricks, but I had to track down about 20 little issues
to get it running under Ruby 1.9. Thank goodness it had terrific test
coverage to lead me to the problem areas.

                                             - James Edward Gray II
  Follow-up at http://blog.grayproductions.net/articles/getting_code_ready_for_ruby_19
Block Local Variables
Arguments are always local

Ruby 1.8                     Ruby 1.9
item = 1                     item = 1
2.upto(4) do |item|          2.upto(4) do |item|
  p item                       p item
end                          end
# Outputs:                   # Outputs:
#    2                       #    2
#    3                       #    3
#    4                       #    4
item                         item
# => 4                       # => 1
Shadowing Variables
You’ll get a warning

Ruby 1.8                     Ruby 1.9
i=1                          i=1
lambda { |i| p i }.call(3)   lambda { |i| p i }.call(3)
# Outputs                    # Outputs
#3                           #3
i                            i
# => 3                       # => 1

                             -e:2: warning: shadowing outer local variable - i
Shadowing Variables
Locals, but warned

No local, reassigns      Local, shadowed
d=2                      d=2
-> { d = 1 }.()          ->(;d) { d = 1 }.()
d                        d
# => 1                   # => 2
            (Ruby 1.9)   -e:2: warning: shadowing outer local variable - d
Hash#select (etc)
Changes to yielded arguments

Ruby 1.8                                                    Ruby 1.9
conferences.select do |data|                               conferences.select do |data|
  p data                                                     p data
end                                                        end
# [:euruko, quot;Praguequot;]                                      # :euruko
# [:scotland_on_rails, quot;Edinburghquot;]                        # :scotland_on_rails
# [:railsconf_europe, quot;Berlinquot;]                            # :railsconf_europe

                                                           conferences.select do |name, city|
warning: multiple values for a block parameter (2 for 1)
                                                             p [name, city]
                                                           end
                                                           # [:euruko, quot;Praguequot;]
                                                           # [:scotland_on_rails, quot;Edinburghquot;]
                                                           # [:railsconf_europe, quot;Berlinquot;]
Hash#select (etc)
Returns a Hash

Ruby 1.8                                   Ruby 1.9
conferences.select do |name, _|            conferences.select do |name, _|
  name == :scotland_on_rails                 name == :scotland_on_rails
end                                        end
# => [[:scotland_on_rails, quot;Edinburghquot;]]   # => {:scotland_on_rails=>quot;Edinburghquot;}
Features
Lots of changes,
some big ones
Multilingualization
(m17n)

There is one type of string, and the encoding is mutable

Strings are no longer Enumerable (use #each_char, #each_line, etc)

The encoding is ‘lazy’ and can be set by probing with
String#ascii_only? and String#valid_encoding?. 

Various ways to set default encoding (commandline, magic
comments)

String#[] now returns a String, not a Fixnum (use ord)
[:ASCII_8BIT, :Big5, :BIG5, :CP949, :EUC_JP, :EUC_KR, :EUC_TW, :GB18030, :GBK, :ISO_885
9_1, :ISO_8859_2, :ISO_8859_3, :ISO_8859_4, :ISO_8859_5, :ISO_8859_6, :ISO_8859_7, :IS
O_8859_8, :ISO_8859_9, :ISO_8859_10, :ISO_8859_11, :ISO_8859_13, :ISO_8859_14, :ISO
_8859_15, :ISO_8859_16, :KOI8_R, :KOI8_U, :Shift_JIS, :SHIFT_JIS, :US_ASCII, :UTF_8, :UTF
_16BE, :UTF_16LE, :UTF_32BE, :UTF_32LE, :Windows_1251, :WINDOWS_1251, :BINARY, :I
BM437, :CP437, :IBM737, :CP737, :IBM775, :CP775, :CP850, :IBM850, :IBM852, :CP852, :IBM85
5, :CP855, :IBM857, :CP857, :IBM860, :CP860, :IBM861, :CP861, :IBM862, :CP862, :IBM863, :CP
863, :IBM864, :CP864, :IBM865, :CP865, :IBM866, :CP866, :IBM869, :CP869, :Windows_1258, :
WINDOWS_1258, :CP1258, :GB1988, :MacCentEuro, :MACCENTEURO, :MacCroatian, :MA
CCROATIAN, :MacCyrillic, :MACCYRILLIC, :MacGreek, :MACGREEK, :MacIceland, :MACICE
LAND, :MacRoman, :MACROMAN, :MacRomania, :MACROMANIA, :MacThai, :MACTHAI, :M
acTurkish, :MACTURKISH, :MacUkraine, :MACUKRAINE, :CP950, :EucJP, :EUCJP, :EucJP_ms, :E
UCJP_MS, :EUC_JP_MS, :CP51932, :EucKR, :EUCKR, :EucTW, :EUCTW, :EUC_CN, :EucCN, :
EUCCN, :GB12345, :CP936, :ISO_2022_JP, :ISO2022_JP, :ISO_2022_JP_2, :ISO2022_JP2, :ISO
8859_1, :Windows_1252, :WINDOWS_1252, :CP1252, :ISO8859_2, :Windows_1250, :WIN
DOWS_1250, :CP1250, :ISO8859_3, :ISO8859_4, :ISO8859_5, :ISO8859_6, :Windows_1256,
:WINDOWS_1256, :CP1256, :ISO8859_7, :Windows_1253, :WINDOWS_1253, :CP1253, :IS
O8859_8, :Windows_1255, :WINDOWS_1255, :CP1255, :ISO8859_9, :Windows_1254, :WI
NDOWS_1254, :CP1254, :ISO8859_10, :ISO8859_11, :TIS_620, :Windows_874, :WINDOW
S_874, :CP874, :ISO8859_13, :Windows_1257, :WINDOWS_1257, :CP1257, :ISO8859_14, :I
SO8859_15, :ISO8859_16, :CP878, :SJIS, :Windows_31J, :WINDOWS_31J, :CP932, :CsWindo
ws31J, :CSWINDOWS31J, :MacJapanese, :MACJAPANESE, :MacJapan, :MACJAPAN, :ASCII, :A
NSI_X3_4_1968, :UTF_7, :CP65000, :CP65001, :UCS_2BE, :UCS_4BE, :UCS_4LE, :CP1251]
Multilingualization
Read a file with File.read


 File.read(quot;input.txtquot;).encoding
 # => #<Encoding:UTF-8>

 File.read(quot;input.txtquot;, encoding: 'ascii-8bit').encoding
 # => #<Encoding:ASCII-8BIT>
Multilingualization
Read a file with File.open

    result = File.open(quot;input.txtquot;, quot;r:euc-jpquot;) do |f|
      f.read
    end
    result.encoding
    # => #<Encoding:EUC-JP>
    result.valid_encoding?
    # => true
Regular Expressions
Integrated “oniguruma” engine

Same basic API

Much better performance

Support for encodings

Extended Syntax
Look-ahead (?=), (?!), look-behind (?<), (?<!)

Named groups (?<>), backreferences, etc
Regular Expressions
Named Groups




quot;His name is Joequot;.match(/name is (?<name>S+)/)[:name]
# => quot;Joequot;
Enumerable
Enumerator built-in, returned from Enumerable methods (and
those in Array, Dir, Hash, IO, Range, String or Struct that serve the
same purposes). Added Enumerator#with_index



                           Map with index
  %w(Joe John Jack).map.with_index do |name, offset|
    quot;#{name} is #{offset + 1}quot;
  end
  # => [quot;Joe is #1quot;, quot;John is #2quot;, quot;Jack is #3quot;]
Enumerable
reduce (inject)




            [1,2,3,4].reduce(:+)
            # => 10
Enumerable
New Enumerable methods take, group_by, drop, min_by, max_by,
count, and others.


        take                            drop

 array = [1, 2, 3, 4, 5]        array = [1, 2, 3, 4, 5]
 array.take(3)                  array.drop(3)
 # => [1, 2, 3]                 # => [4, 5]
 array                          array
 # => [1, 2, 3, 4, 5]           # => [1, 2, 3, 4, 5]
Hash Changes
Insertion order preserved
conferences = {
  euruko: 'Prague',
  scotland_on_rails: 'Edinburgh'
}
conferences[:railsconf_europe] = 'Berlin'
conferences.each do |name, city|
  p quot;#{name} is in #{city}quot;
end
# quot;euruko is in Praguequot;
# quot;scotland_on_rails is in Edinburghquot;
# quot;railsconf_europe is in Berlinquot;
conferences.delete(:scotland_on_rails)
conferences[:scotland_on_rails] = 'Edinburgh'
conferences.each do |name, city|
  p quot;#{name} is in #{city}quot;
end
# quot;euruko is in Praguequot;
# quot;railsconf_europe is in Berlinquot;
# quot;scotland_on_rails is in Edinburghquot;
Object
Added tap



      thing = Thing.new.tap do |thing|
        thing.something = 1
        thing.something_else = 2
      end
Lambda Changes
Obfuscation, ahoy!
New literal syntax more flexible

Not possible in { | | ... } style literals


Passing blocks                               Default arguments
m = ->(x, &b) { b.(x * 2) if b }         ->(a, b=2) { a * b }.(3)
m.(3) do |result|
                                         # => 6
  puts result
end
# Output
#6
Symbol Changes
Less sibling rivalry

Added to_proc

Added =~, [] like String (to_s less needed), sortable

Object#methods, etc now return an array of symbols


Indexing into                     Comparing with a String
:foo[1]                           :this === quot;thisquot;
                                  # => true
# => quot;oquot;
Fibers
“Semi-coroutines”

Similar to Python’s generators
Owe method naming lineage to Lua
Out of scope of the talk, but very cool
For some examples, see:
http://pragdave.blogs.pragprog.com/pragdave/2007/12/pipelines-using.html (and follow-up)

http://www.davidflanagan.com/blog/2007_08.html (older)

Revactor project (Actors in 1.9 using Fibers + Threads)

InfoQ, others...
This was really just an introduction.




Bruce Williams                              twitter: wbruce
                  bruce@codefluency.com

Contenu connexe

Tendances

Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Rails
elliando dias
 
Hacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 AutumnHacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 Autumn
Moriyoshi Koizumi
 
Declarative Internal DSLs in Lua: A Game Changing Experience
Declarative Internal DSLs in Lua: A Game Changing ExperienceDeclarative Internal DSLs in Lua: A Game Changing Experience
Declarative Internal DSLs in Lua: A Game Changing Experience
Alexander Gladysh
 
Kai – An Open Source Implementation of Amazon’s Dynamo
Kai – An Open Source Implementation of Amazon’s DynamoKai – An Open Source Implementation of Amazon’s Dynamo
Kai – An Open Source Implementation of Amazon’s Dynamo
Takeru INOUE
 

Tendances (20)

Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Rails
 
Ruby meets Go
Ruby meets GoRuby meets Go
Ruby meets Go
 
Objective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central DispatchObjective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central Dispatch
 
Opaque Pointers Are Coming
Opaque Pointers Are ComingOpaque Pointers Are Coming
Opaque Pointers Are Coming
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Python
 
Hacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 AutumnHacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 Autumn
 
Writing Parsers and Compilers with PLY
Writing Parsers and Compilers with PLYWriting Parsers and Compilers with PLY
Writing Parsers and Compilers with PLY
 
PHP Performance Trivia
PHP Performance TriviaPHP Performance Trivia
PHP Performance Trivia
 
Runtime Symbol Resolution
Runtime Symbol ResolutionRuntime Symbol Resolution
Runtime Symbol Resolution
 
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur..."How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
 
Declarative Internal DSLs in Lua: A Game Changing Experience
Declarative Internal DSLs in Lua: A Game Changing ExperienceDeclarative Internal DSLs in Lua: A Game Changing Experience
Declarative Internal DSLs in Lua: A Game Changing Experience
 
RubyKaigi2015 making robots-with-mruby
RubyKaigi2015 making robots-with-mrubyRubyKaigi2015 making robots-with-mruby
RubyKaigi2015 making robots-with-mruby
 
Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) Things
 
HKG15-211: Advanced Toolchain Usage Part 4
HKG15-211: Advanced Toolchain Usage Part 4HKG15-211: Advanced Toolchain Usage Part 4
HKG15-211: Advanced Toolchain Usage Part 4
 
Kai – An Open Source Implementation of Amazon’s Dynamo
Kai – An Open Source Implementation of Amazon’s DynamoKai – An Open Source Implementation of Amazon’s Dynamo
Kai – An Open Source Implementation of Amazon’s Dynamo
 
HKG15-207: Advanced Toolchain Usage Part 3
HKG15-207: Advanced Toolchain Usage Part 3HKG15-207: Advanced Toolchain Usage Part 3
HKG15-207: Advanced Toolchain Usage Part 3
 
Aspect Mining for Large Systems
Aspect Mining for Large SystemsAspect Mining for Large Systems
Aspect Mining for Large Systems
 
Intro to J Ruby
Intro to J RubyIntro to J Ruby
Intro to J Ruby
 

En vedette (6)

The road to war, the aesthetics of anguish
The road to war, the aesthetics of anguishThe road to war, the aesthetics of anguish
The road to war, the aesthetics of anguish
 
The Human Side of UX Design (WordCamp Toronto)
The Human Side of UX Design (WordCamp Toronto)The Human Side of UX Design (WordCamp Toronto)
The Human Side of UX Design (WordCamp Toronto)
 
Lm741
Lm741Lm741
Lm741
 
Arrowsmith Builders
Arrowsmith BuildersArrowsmith Builders
Arrowsmith Builders
 
Tarea2 Ea2
Tarea2 Ea2Tarea2 Ea2
Tarea2 Ea2
 
OruAcre.com Marketing
OruAcre.com MarketingOruAcre.com Marketing
OruAcre.com Marketing
 

Similaire à Migrating To Ruby1.9

Similaire à Migrating To Ruby1.9 (20)

Hacking with ruby2ruby
Hacking with ruby2rubyHacking with ruby2ruby
Hacking with ruby2ruby
 
How to check valid Email? Find using regex.
How to check valid Email? Find using regex.How to check valid Email? Find using regex.
How to check valid Email? Find using regex.
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
 
Ruby 1.9
Ruby 1.9Ruby 1.9
Ruby 1.9
 
React Native Evening
React Native EveningReact Native Evening
React Native Evening
 
Cより速いRubyプログラム
Cより速いRubyプログラムCより速いRubyプログラム
Cより速いRubyプログラム
 
Quick Intro To JRuby
Quick Intro To JRubyQuick Intro To JRuby
Quick Intro To JRuby
 
A Recovering Java Developer Learns to Go
A Recovering Java Developer Learns to GoA Recovering Java Developer Learns to Go
A Recovering Java Developer Learns to Go
 
Ruby on Rails 3.1: Let's bring the fun back into web programing
Ruby on Rails 3.1: Let's bring the fun back into web programingRuby on Rails 3.1: Let's bring the fun back into web programing
Ruby on Rails 3.1: Let's bring the fun back into web programing
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
 
Groovy
GroovyGroovy
Groovy
 
Adventurous Merb
Adventurous MerbAdventurous Merb
Adventurous Merb
 
Ruby 1.9 Introduction
Ruby 1.9 IntroductionRuby 1.9 Introduction
Ruby 1.9 Introduction
 
An introduction to Ruby
An introduction to RubyAn introduction to Ruby
An introduction to Ruby
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentation
 
Practical Groovy DSL
Practical Groovy DSLPractical Groovy DSL
Practical Groovy DSL
 
Google's HTML5 Work: what's next?
Google's HTML5 Work: what's next?Google's HTML5 Work: what's next?
Google's HTML5 Work: what's next?
 
Node Boot Camp
Node Boot CampNode Boot Camp
Node Boot Camp
 

Dernier

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Dernier (20)

Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 

Migrating To Ruby1.9

  • 1. Migrating to Ruby 1.9 Bruce Williams
  • 2. Bruce Williams Perpetrator of much random Ruby hackery, language tourist Rubyist since 2001 (Full-time since 2005) Open source developer, contributer, technical editor, designer Occasionally blogs at http://codefluency.com
  • 3. Ruby 1.8 Ruby 1.9 Stable. Unstable, transitional. The syntax and language Many new syntax and features you know and language features. probably love. Better performance, The performance profile especially for computationally you know and might hate intensive operations. a little.
  • 4. 1.9 is a hint.
  • 5. 1.9 is a hint. Get ready for 2.0.
  • 6. Ruby’s Releases From Toybox to Toolshed (dev) (dev) 1.7 (dev) 1.6.1 1.6.3 1.6.5 1.8.1 1.8.4 1.5 1.9 1.6.0 1.6.2 1.6.4 1.6.7 1.6.8 1.8.0 1.8.2 1.8.5 1.8.6 1.8.3 ‘00 ‘01 ‘02 ‘03 ‘04 ‘05 ‘06 ‘07 ‘08 Japan Beyond Japan “... on Rails” Expansion
  • 7. Standard Library rubygems (+ prelude & ruby --disable-gems), rake, json (pure, + ext), ripper, probeprofiler, securerandom, HMAC digests ~ csv replaced by FasterCSV implementation - soap, wsdl, base64, some rarely used, old libraries
  • 8. Parser Changes Flexibility and Obscurity (for the moment)
  • 9. Parser Changes New Hash Literal {a: quot;fooquot;} # => {:a=>quot;fooquot;} {a: quot;barquot;, :b => quot;bazquot;} # => {:a=>quot;barquot;, :b=>quot;bazquot;}
  • 10. Parser Changes New Proc Literal multiply_by_2 = ->(x) { x * 2 } # => #<Proc:0x3c5a50> multiply_by_2.(4) # => 8
  • 11. Parser Changes Splat more flexibly names = %w(joe john bill) [*names, 'jack'] # => [quot;joequot;, quot;johnquot;, quot;billquot;, quot;jackquot;]
  • 12. Parser Changes Method parameter ordering def say(language=:english, text) puts Translator[language].translate(text) end say quot;helloquot; # hello say :spanish, quot;helloquot; # hola
  • 13. Migration Risk Factors Text processing “Clever” assignment with blocks Some Hash enumerations Metaprogramming, code generation
  • 14. Tests are Good I was surprised at how much work my 11th hour integration of the FasterCSV code was. It was a pure Ruby library that really didn't do a lot of fancy tricks, but I had to track down about 20 little issues to get it running under Ruby 1.9. Thank goodness it had terrific test coverage to lead me to the problem areas. - James Edward Gray II Follow-up at http://blog.grayproductions.net/articles/getting_code_ready_for_ruby_19
  • 15. Block Local Variables Arguments are always local Ruby 1.8 Ruby 1.9 item = 1 item = 1 2.upto(4) do |item| 2.upto(4) do |item| p item p item end end # Outputs: # Outputs: # 2 # 2 # 3 # 3 # 4 # 4 item item # => 4 # => 1
  • 16. Shadowing Variables You’ll get a warning Ruby 1.8 Ruby 1.9 i=1 i=1 lambda { |i| p i }.call(3) lambda { |i| p i }.call(3) # Outputs # Outputs #3 #3 i i # => 3 # => 1 -e:2: warning: shadowing outer local variable - i
  • 17. Shadowing Variables Locals, but warned No local, reassigns Local, shadowed d=2 d=2 -> { d = 1 }.() ->(;d) { d = 1 }.() d d # => 1 # => 2 (Ruby 1.9) -e:2: warning: shadowing outer local variable - d
  • 18. Hash#select (etc) Changes to yielded arguments Ruby 1.8 Ruby 1.9 conferences.select do |data| conferences.select do |data| p data p data end end # [:euruko, quot;Praguequot;] # :euruko # [:scotland_on_rails, quot;Edinburghquot;] # :scotland_on_rails # [:railsconf_europe, quot;Berlinquot;] # :railsconf_europe conferences.select do |name, city| warning: multiple values for a block parameter (2 for 1) p [name, city] end # [:euruko, quot;Praguequot;] # [:scotland_on_rails, quot;Edinburghquot;] # [:railsconf_europe, quot;Berlinquot;]
  • 19. Hash#select (etc) Returns a Hash Ruby 1.8 Ruby 1.9 conferences.select do |name, _| conferences.select do |name, _| name == :scotland_on_rails name == :scotland_on_rails end end # => [[:scotland_on_rails, quot;Edinburghquot;]] # => {:scotland_on_rails=>quot;Edinburghquot;}
  • 21. Multilingualization (m17n) There is one type of string, and the encoding is mutable Strings are no longer Enumerable (use #each_char, #each_line, etc) The encoding is ‘lazy’ and can be set by probing with String#ascii_only? and String#valid_encoding?.  Various ways to set default encoding (commandline, magic comments) String#[] now returns a String, not a Fixnum (use ord)
  • 22. [:ASCII_8BIT, :Big5, :BIG5, :CP949, :EUC_JP, :EUC_KR, :EUC_TW, :GB18030, :GBK, :ISO_885 9_1, :ISO_8859_2, :ISO_8859_3, :ISO_8859_4, :ISO_8859_5, :ISO_8859_6, :ISO_8859_7, :IS O_8859_8, :ISO_8859_9, :ISO_8859_10, :ISO_8859_11, :ISO_8859_13, :ISO_8859_14, :ISO _8859_15, :ISO_8859_16, :KOI8_R, :KOI8_U, :Shift_JIS, :SHIFT_JIS, :US_ASCII, :UTF_8, :UTF _16BE, :UTF_16LE, :UTF_32BE, :UTF_32LE, :Windows_1251, :WINDOWS_1251, :BINARY, :I BM437, :CP437, :IBM737, :CP737, :IBM775, :CP775, :CP850, :IBM850, :IBM852, :CP852, :IBM85 5, :CP855, :IBM857, :CP857, :IBM860, :CP860, :IBM861, :CP861, :IBM862, :CP862, :IBM863, :CP 863, :IBM864, :CP864, :IBM865, :CP865, :IBM866, :CP866, :IBM869, :CP869, :Windows_1258, : WINDOWS_1258, :CP1258, :GB1988, :MacCentEuro, :MACCENTEURO, :MacCroatian, :MA CCROATIAN, :MacCyrillic, :MACCYRILLIC, :MacGreek, :MACGREEK, :MacIceland, :MACICE LAND, :MacRoman, :MACROMAN, :MacRomania, :MACROMANIA, :MacThai, :MACTHAI, :M acTurkish, :MACTURKISH, :MacUkraine, :MACUKRAINE, :CP950, :EucJP, :EUCJP, :EucJP_ms, :E UCJP_MS, :EUC_JP_MS, :CP51932, :EucKR, :EUCKR, :EucTW, :EUCTW, :EUC_CN, :EucCN, : EUCCN, :GB12345, :CP936, :ISO_2022_JP, :ISO2022_JP, :ISO_2022_JP_2, :ISO2022_JP2, :ISO 8859_1, :Windows_1252, :WINDOWS_1252, :CP1252, :ISO8859_2, :Windows_1250, :WIN DOWS_1250, :CP1250, :ISO8859_3, :ISO8859_4, :ISO8859_5, :ISO8859_6, :Windows_1256, :WINDOWS_1256, :CP1256, :ISO8859_7, :Windows_1253, :WINDOWS_1253, :CP1253, :IS O8859_8, :Windows_1255, :WINDOWS_1255, :CP1255, :ISO8859_9, :Windows_1254, :WI NDOWS_1254, :CP1254, :ISO8859_10, :ISO8859_11, :TIS_620, :Windows_874, :WINDOW S_874, :CP874, :ISO8859_13, :Windows_1257, :WINDOWS_1257, :CP1257, :ISO8859_14, :I SO8859_15, :ISO8859_16, :CP878, :SJIS, :Windows_31J, :WINDOWS_31J, :CP932, :CsWindo ws31J, :CSWINDOWS31J, :MacJapanese, :MACJAPANESE, :MacJapan, :MACJAPAN, :ASCII, :A NSI_X3_4_1968, :UTF_7, :CP65000, :CP65001, :UCS_2BE, :UCS_4BE, :UCS_4LE, :CP1251]
  • 23. Multilingualization Read a file with File.read File.read(quot;input.txtquot;).encoding # => #<Encoding:UTF-8> File.read(quot;input.txtquot;, encoding: 'ascii-8bit').encoding # => #<Encoding:ASCII-8BIT>
  • 24. Multilingualization Read a file with File.open result = File.open(quot;input.txtquot;, quot;r:euc-jpquot;) do |f| f.read end result.encoding # => #<Encoding:EUC-JP> result.valid_encoding? # => true
  • 25. Regular Expressions Integrated “oniguruma” engine Same basic API Much better performance Support for encodings Extended Syntax Look-ahead (?=), (?!), look-behind (?<), (?<!) Named groups (?<>), backreferences, etc
  • 26. Regular Expressions Named Groups quot;His name is Joequot;.match(/name is (?<name>S+)/)[:name] # => quot;Joequot;
  • 27. Enumerable Enumerator built-in, returned from Enumerable methods (and those in Array, Dir, Hash, IO, Range, String or Struct that serve the same purposes). Added Enumerator#with_index Map with index %w(Joe John Jack).map.with_index do |name, offset| quot;#{name} is #{offset + 1}quot; end # => [quot;Joe is #1quot;, quot;John is #2quot;, quot;Jack is #3quot;]
  • 28. Enumerable reduce (inject) [1,2,3,4].reduce(:+) # => 10
  • 29. Enumerable New Enumerable methods take, group_by, drop, min_by, max_by, count, and others. take drop array = [1, 2, 3, 4, 5] array = [1, 2, 3, 4, 5] array.take(3) array.drop(3) # => [1, 2, 3] # => [4, 5] array array # => [1, 2, 3, 4, 5] # => [1, 2, 3, 4, 5]
  • 30. Hash Changes Insertion order preserved conferences = { euruko: 'Prague', scotland_on_rails: 'Edinburgh' } conferences[:railsconf_europe] = 'Berlin' conferences.each do |name, city| p quot;#{name} is in #{city}quot; end # quot;euruko is in Praguequot; # quot;scotland_on_rails is in Edinburghquot; # quot;railsconf_europe is in Berlinquot; conferences.delete(:scotland_on_rails) conferences[:scotland_on_rails] = 'Edinburgh' conferences.each do |name, city| p quot;#{name} is in #{city}quot; end # quot;euruko is in Praguequot; # quot;railsconf_europe is in Berlinquot; # quot;scotland_on_rails is in Edinburghquot;
  • 31. Object Added tap thing = Thing.new.tap do |thing| thing.something = 1 thing.something_else = 2 end
  • 32. Lambda Changes Obfuscation, ahoy! New literal syntax more flexible Not possible in { | | ... } style literals Passing blocks Default arguments m = ->(x, &b) { b.(x * 2) if b } ->(a, b=2) { a * b }.(3) m.(3) do |result| # => 6 puts result end # Output #6
  • 33. Symbol Changes Less sibling rivalry Added to_proc Added =~, [] like String (to_s less needed), sortable Object#methods, etc now return an array of symbols Indexing into Comparing with a String :foo[1] :this === quot;thisquot; # => true # => quot;oquot;
  • 34. Fibers “Semi-coroutines” Similar to Python’s generators Owe method naming lineage to Lua Out of scope of the talk, but very cool For some examples, see: http://pragdave.blogs.pragprog.com/pragdave/2007/12/pipelines-using.html (and follow-up) http://www.davidflanagan.com/blog/2007_08.html (older) Revactor project (Actors in 1.9 using Fibers + Threads) InfoQ, others...
  • 35. This was really just an introduction. Bruce Williams twitter: wbruce bruce@codefluency.com