ruby 1.9

Download Ruby 1.9

Post on 12-Sep-2014

17.472 views

Category:

Documents

6 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

  • Ruby 1.9: It works!ihower@handlino.com

    2009/4/18OSDC.TW

    2009419

  • Ruby 1.9: It works!ihower@handlino.com

    2009/4/18OSDC.TW

    (must)

    2009419

  • ? a.k.a. ihower http://ihower.idv.tw/blog/ http://twitter.com/ihower

    2006 Ruby () Rails Developer http://handlino.com http://registrano.com

    2009419

  • Agenda

    Overview Whats changed? Whats New?

    (External Interater, M17N, The Threading Model)

    Ruby 1.9 on Rails

    2009419

  • 1. Overview

    2009419

  • Ruby 1.8.6

    Ruby 1.8.6, release at 2007/3/12 Stable Performance sucks Memory leaks NewRelic Report 69.4%

    http://railslab.newrelic.com/2009/04/01/the-state-of-the-stack-a-ruby-on-rails-benchmarking-report

    2009419

  • Ruby 1.8.7

    Ruby 1.8.7 release at 2008/5/31 1.9 gems, library (1.8.6) JRuby 1.8.7 NewRelic Report 14.5%

    2009419

  • Ruby 1.9

    Ruby 1.9.0 release development version in 2007/12/25

    Ruby 1.9.1 release stable/production version in 2009/1/30

    Better performance Better encoding support

    2009419

  • Ruby Implementations production

    Ruby 1.8.6, 1.8.7 (MRI, Matz Ruby Interpreter) EngineYard 1.8.6

    MBARI GC patch

    Ruby 1.9.1 (YARV) JRuby

    Ruby1.8.6 1.9

    Ruby Enterprise Edition(REE) Ruby 1.8.6 GC memory leak

    http://www.infoq.com/news/2009/01/ruby-patches-fix-leaks

    2009419

  • Ruby Implementations production

    MacRuby (based on Objective-C) Rubinius (Engine yard project) MagLev (based on smalltalk) IronRuby (based on Microsoft .NET) Cardinal (based on Parrot VM)

    2009419

  • Performance ( 1.8 2~2.5 )

    http://antoniocangiano.com/category/ruby-benchmark-suite/http://github.com/acangiano/ruby-benchmark-suite/

    2009419

  • Ubuntu : Intel Q6600 quad-coreComputer Language Benchmarks Game

    http://shootout.alioth.debian.org/

    2009419

  • 2. Whats changed?

    2009419

  • Ordered Hash

    {:a=>1, :d=>4, :b=>2, :c=>3}1.8

    1.9

    { :a => 1, :b=> 2, :c => 3 }.merge( :d => 4 )

    {:a=>1, :b=>2, :c=>3, :d=>4}

    2009419

  • new Hash literal

    config = { :foo => 1234, :bar => 6789 }

    config = { foo: 1234, bar: 6789 }

    1.8,1.9

    1.9

    Person.find(:all, :conditions => { :name => 'ihower' } )

    Person.find(:all, :conditions => { name: 'ihower' } )

    2009419

  • Hash syntax =>

    1.8 {1,2,3,4}=> {1=>2, 3=>4}

    {1,2,3,4}=> syntax error, unexpected ',', expecting tASSOC1.9

    2009419

  • Hash#selecthash = { :a => 1, :b => 2, :c => 3 }hash.select{ |k, v| v > 1 }

    [[:b, 2], [:c, 3]]

    {:b=>2, :c=>3}

    1.8

    1.9

    2009419

  • Array#to_s, Hash#to_s

    [1,2,3,4].to_s => "1234" {1=>2,3=>4}.to_s => "1234"

    [1,2,3,4].to_s => "[1, 2, 3, 4]" {1=>2,3=>4}.to_s => "{1=>2, 3=>4}"

    1.91.8

    2009419

  • case syntax

    name = case when x == 1 : "one" when x == 2 : "two" else "many" end

    name = case when x == 1 then "one" when x == 2 then "two" else "many" end

    name = case when x == 1 "one" when x == 2 "two" else "many" end

    1.8 1.8,1.9

    1.8,1.9

    2009419

  • blocks parameter always local local

    x = "foo"y = "bar"

    [1,2,3].each do |x| y=x+1end

    1.9[x,y] => [3, 4] [x,y] => ["foo", 4]1.8

    2009419

  • blocks parameter always local local

    x = "foo"y = "bar"

    [1,2,3].each do |x| y=x+1end

    1.9[x,y] => [3, 4] [x,y] => ["foo", 4]1.8

    2009419

  • block-local variable local

    bar = "ihower"

    [1,2,3].each do |val| foo = val bar = val end

    foo # NameError: undefined local variable or method `foobar => 3

    bar = "ihower"

    [1,2,3].each do |val; bar| bar = valend

    bar => "ihower"

    1.91.8,1.9

    2009419

  • block-local variable local

    bar = "ihower"

    [1,2,3].each do |val| foo = val bar = val end

    foo # NameError: undefined local variable or method `foobar => 3

    bar = "ihower"

    [1,2,3].each do |val; bar| bar = valend

    bar => "ihower"

    1.91.8,1.9

    bar

    2009419

  • block can accept block argrments

    proc1 = lambda do |a, *b, &block| p a p b p c block.callend

    proc1.call(1,2,3,4) { puts "in block1" }

    # 1[2,3,4]"in block1"

    2009419

  • new lambda literal lambda

    ->(a) { a*3 }.(4)# => 12

    lambda { |a| a*3 }.call(4)# => 12

    lambda { |a| a*3 }[4]# => 12

    1.8,1.9

    1.8,1.9

    1.9

    2009419

  • def my_if(condition, then_clause, else_clause) if condition then_clause.call else else_clause.call endend

    5.times do |val| my_if (val < 3), -> { puts "#{val} is small" }, -> { puts "#{val} is big" }end

    # 0 is small1 is small2 is small3 is big4 is big

    2009419

  • def my_while(cond, &body) while cond.call body.call endend

    a = 0my_while -> { a < 3 } do puts a a += 1end

    # 012

    2009419

  • Kernel.proc, do not use it!

    proc lambda proc Proc.new1.9

    1.8

    2009419

  • BasicObject root

    Class.superclass.superclass.superclass => nil

    Class.superclass.superclass.superclass => BasicObjectBasicObject.superclass => nil1.9

    Class.superclass.superclass => Object

    BasicObject.instance_methods=> [:==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__]

    1.8

    1.8,1.9

    metaprogramming blank canvas

    2009419

  • standard library changes + Rubygems

    no longer needed require rubygems

    + Rake no longer gem install rake

    ~ Test::Unit replaced by MiniTest - soap, jcode...etc, some rarely used, old

    libraries

    2009419

  • 3. Whats new?

    2009419

  • External Iterator

    2009419

  • Iterators to_enum

    arr = [ 1, 2, "ihower" ]enum_a = arr.to_enum # Enumerators (each)

    enum_a.next # => 1enum_a.next # => 2enum_a.next # => "ihower"

    ExternalInternalarr = [ 1, 2, "ihower" ]arr.each do |a| puts a end

    # => 1# => 2# => "ihower"

    1.91.8,1.9

    2009419

  • Enumerators

    h = { foo: "567", bar: "890" }enum_h = h.to_enumenum_h.next # => [:foo, "567"]enum_h.next # => [:bar, "890"]

    arr = [ 1, 2, "ihower" ]enum_a = arr.to_enum(:each_with_index)

    enum_a.next # => [1, 0]enum_a.next # => [2, 1]enum_a.next # => ["ihower", 2]

    1.9 1.9

    2009419

  • StopIteration exception

    short_enum = [1, 2, 3].to_enumlong_enum = ('a'..'z').to_enum

    loop do puts "#{short_enum.next} #{long_enum.next}"end

    # => 1-a# => 2-b# => 3-c

    1.9

    2009419

  • StopIteration exception

    short_enum = [1, 2, 3].to_enumlong_enum = ('a'..'z').to_enum

    loop do puts "#{short_enum.next} #{long_enum.next}"end

    # => 1-a# => 2-b# => 3-c

    StopIteration

    1.9

    2009419

  • Enumerator from iterator iterator method

    arr = [ 1, 2, "ihower" ]enum_a = arr.each # Enumerator

    enum_a.next # => 1enum_a.next # => 2enum_a.next # => "ihower"

    1.9

    2009419

  • Enumerators.new code block

    seq = Enumerator.new do |yielder| n1 = 0 n2 = 1 loop do n3 = n1 + n2 yielder.yield n3 n1 = n2 n2 = n3 endend

    seq.next # 1seq.next # 22.times { puts seq.next }# 3# 5

    1.9

    2009419

  • Enumerator objects are also enumerable

    seq.first(10)# => [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

    seq.to_a# Dont do this if your Enumerator has infinite elements

    1.9

    2009419

  • M17N(Multilingualization)

    2009419

  • In Ruby 1.8, a String is always just a collection of bytes

    2009419

  • Ruby 1.8 String fails>> "".reverse=> "\207\226\344"

    >> "".size=> 6

    1.8

    2009419

  • Ruby 1.8 Regexp UTF-8

    Support None (n or N), EUC (e or E), Shift_JIS (s or S), UTF-8 (u or U)

    >> "".scan(/./u)=> ["", ""]

    1.8,1.9

    2009419

  • Use Regexp to handle String Regexp

    >> "".scan(/./u).reverse.join=> ""

    >> "".scan(/./u).size=> 2

    1.8,1.9

    2009419

  • $KCODE = Uor ruby -KU

    regular expressions default encoding $KCODE = "U" terminal

    >> ""=> "\344\270\255\346\226\207">> $KCODE='u'>> ""=> ""

    1.8

    2009419

  • Unicode codepoint Ruby 1.8 UTF-8

    >> "".unpack("U*")=> [20013, 25991]

    >> "".unpack("U*").reverse.pack("U*")=> ""

    >> "".unpack("U*").size=> 2

    1.8,1.9

    2009419

  • Unicode codepoint(cont.)Rails ActiveSupport

    >> "".chars # in Rails=> #

    >> "".chars.size => 2

    1.8,1.9

    2009419

  • iconv library UTF-8 data

    C Library, its fast. convert your encoding to UTF-8 handle it export back to your encoding

    2009419

  • jcode librarytoo simple, not useful

    1.8 $KCODE = 'UTF8'require 'jcode'

    str = ""str.size # 9str.jsize # 2

    2009419

  • No enough encodings supported in Ruby 1.8

    String Encoding Regexp

    encoding Regexp letter #String.upcaseencoded data valid?

    $KCODE S