Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads.
Ruby 1.9: It works!
    ihower@handlino.com
         2009/4/18
         OSDC.TW
Ruby 1.9: It (must) works!
       ihower@handlino.com
            2009/4/18
            OSDC.TW
?
•           a.k.a. ihower
•   http://ihower.idv.tw/blog/

•   http://.com/ihower

•     2006                  Rub...
Agenda
• Overview
• What’s changed?
• What’s New?
  (External Interater, M17N, The Threading Model)


• Ruby 1.9 on Rails
1. Overview
Ruby 1.8.6
• Ruby 1.8.6, release at 2007/3/12
• Stable
• Performance sucks
• Memory leaks
• NewRelic Report 69.4%
  http:/...
Ruby 1.8.7

• Ruby 1.8.7 release at 2008/5/31
•               1.9
• gems, library            (   1.8.6   )


• JRuby 1.8.7...
Ruby 1.9

• Ruby 1.9.0 release development version in
  2007/12/25
• Ruby 1.9.1 release stable/production version
  in 200...
Ruby Implementations
                           production

•   Ruby 1.8.6, 1.8.7 (MRI, Matz’ Ruby Interpreter)
    •   En...
Ruby Implementations
                   production


• MacRuby (based on Objective-C)
• Rubinius (Engine yard project)
• M...
Performance                   (    1.8   2~2.5   )




http://antoniocangiano.com/category/ruby-benchmark-suite/
     http...
Ubuntu : Intel® Q6600® quad-core
Computer Language Benchmarks Game

http://shootout.alioth.debian.org/
2. What’s changed?
Ordered Hash
{ :a => 1, :b=> 2, :c => 3 }.merge( :d => 4 )




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



     1.9   {:a=>...
new Hash literal

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


          Person.find(:all, :conditions => {...
Hash syntax
                                 =>


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


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


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


  ...
Array#to_s, Hash#to_s
 1.8                1.9

 [1,2,3,4].to_s     [1,2,3,4].to_s
 => quot;1234quot;          => quot;[1, ...
case syntax
name = case                        name = case
        when x == 1 : quot;onequot;                  when x == ...
block’s parameter always local
                             local

               x = quot;fooquot;
               y = quo...
block’s parameter always local
                             local

               x = quot;fooquot;
               y = quo...
block-local variable
                                                             local

                                 ...
block-local variable
                                                             local

                                 ...
block can accept block argrments

  proc1 = lambda do |a, *b, &block|
   pa
   pb
   pc
   block.call
  end

  proc1.call(...
new lambda literal
                  lambda

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


      ...
def my_if(condition, then_clause, else_clause)
    if condition
        then_clause.call
    else
        else_clause.call...
def my_while(cond, &body)
    while cond.call
        body.call
    end
end

a=0
my_while -> { a < 3 } do
    puts a
    a...
Kernel.proc
           , do not use it!


  • proc           lambda
1.8

  •
1.9 proc           Proc.new
BasicObject
                                             root


          Class.superclass.superclass => Object
1.8,1.9


...
standard library changes
• + Rubygems
  •   no longer needed require ‘rubygems’


• + Rake
  •   no longer gem install rak...
3. What’s new?
External Iterator
Iterators
                                 to_enum

          Internal               External
                           1...
Enumerators
1.9                              1.9
                                 arr = [ 1, 2, quot;ihowerquot; ]
h = { f...
StopIteration exception
1.9
      short_enum = [1, 2, 3].to_enum
      long_enum = ('a'..'z').to_enum

      loop do
     ...
StopIteration exception
1.9
      short_enum = [1, 2, 3].to_enum
      long_enum = ('a'..'z').to_enum

      loop do
     ...
Enumerator from iterator
                          iterator method
1.9
      arr = [ 1, 2, quot;ihowerquot; ]
      enum_a...
Enumerators.new
              code block
1.9   seq = Enumerator.new do |yielder|
          n1 = 0
          n2 = 1
       ...
Enumerator objects are
    also enumerable
1.9
      seq.first(10)
      # => [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

      s...
M17N
(Multilingualization)
In Ruby 1.8, a String is always
   just a collection of bytes
Ruby 1.8 String fails
        >> quot;   quot;.reverse
  1.8
        => quot;207226 344quot;

        >> quot;   quot;.siz...
Ruby 1.8 Regexp
                          UTF-8


• Support None (n or N), EUC (e or E),
  Shift_JIS (s or S), UTF-8 (u or...
Use Regexp to handle String
                    Regexp



          >> quot;   quot;.scan(/./u).reverse.join
1.8,1.9

    ...
$KCODE = “U”
                      or ruby -KU

•    regular expressions            default encoding
• $KCODE = quot;Uquot...
Unicode codepoint
                 Ruby 1.8         UTF-8

          >> quot;   quot;.unpack(quot;U*quot;)
1.8,1.9
       ...
Unicode codepoint(cont.)
              Rails ActiveSupport


           >> quot;   quot;.chars # in Rails
 1.8,1.9
       ...
iconv library
                            UTF-8 data


•   C Library, it’s fast.

•   convert your encoding to UTF-8

•   ...
jcode library
      too simple, not useful

        $KCODE = 'UTF8'
1.8
        require 'jcode'

        str = quot;   quo...
No enough encodings
           supported in Ruby 1.8
• String                Encoding
• Regexp
 •           encoding      ...
pick one encoding, likely Unicode, and works all data in one format? No.

Ruby 1.9 make it possible to work
  with data wi...
>> Encoding.name_list

=> [quot;ASCII-8BITquot;, quot;UTF-8quot;, quot;US-ASCIIquot;, quot;Big5quot;, quot;CP949quot;, quo...
All String are Encoded
• In Ruby 1.9 a String is a collection of
  encoded characters.
           raw bytes     Encoding

...
String works in characters
       1.9   >> quot;   quot;.reverse
             => quot;   quot;

             >> quot;   qu...
String Indexing
                    1.9
1.8
      >> quot;abcquot;[0]     >> quot;abcquot;[0]
      => 97           => quo...
!

In Ruby 1.9, String has attached
 Encoding object, and works in
          characters.
Transcoding
      utf8 = quot;   quot;
1.9
      utf8.bytesize # 6
      utf8.bytes.to_a # [230, 184, 172, 232, 169, 166]
...
Transcoding fails
      str = quot;Résuméquot;
1.9   str.encode(quot;big5quot;)

      => Encoding::UndefinedConversionErr...
Force Transcoding
                          byte data

      utf8 = quot;   quot;
1.9
      big5 = utf8.encode(quot;big5qu...
Force Transcoding fails
      big5.valid_encoding? # false
1.9   big5 =~ /123456/

      => ArgumentError: invalid byte se...
Encoding.compatible?
                : ASCII with a bigger Encoding

       ascii = quot;my quot;.force_encoding(quot;asci...
Encoding.compatible?
      big5 = quot;   quot;.encode(quot;big5quot;)
1.9
      utf8 = quot;Résuméquot;

      #
      En...
String Iteration
               each

 • Strings are no longer enumerable
 • Ruby 1.8 each() has been removed
 •
1.9 Ruby ...
What’s default encoding?
       ?   encoding   ?
program source encoding
                magic comment

    # encoding: UTF-8

    #!/usr/bin/env ruby -w
    # encoding: U...
program without magic comment

ruby foobar.rb # US-ASCII

ruby -e foobar.rb     irb #   OS   ($LC_CTYPE, $LANG)


ruby -KU...
program source
      encoding           error

invalid multibyte char (US-ASCII)
IO Object
1.9
      # encoding: utf-8
      f = File.open(quot;big5.txtquot;,quot;r:big5quot;)
      puts f.external_encod...
IO Object
1.9
      # encoding: utf-8
      f = File.open(quot;big5.txtquot;,quot;r:big5:utf-8quot;)
      puts f.external...
IO Object
1.9
      # encoding: utf-8
      f = File.open(quot;another_big5.txtquot;, quot;w:big5quot;)
      puts f.exter...
IO Object
1.9
      # encoding: utf-8
      f = File.open(quot;another_big5.txtquot;, quot;w:big5quot;)
      puts f.exter...
Regexp has encoding too
                          • Oniguruma engine
1.9
                            http://www.geocities....
Default Encoding Overview

• String literal program source
• Symbols Regexp                             7-bit
  US-ASCII  ...
The Threading model
Fibers
    (semi-coroutine)
           code block
f = Fiber.new {
1.9
          2.times do
              puts quot;Fiber say hiquot;
              Fiber.yield #
           ...
Fibers
•        Enumerator
•        fiber library                                  fiber
    object
• (Ruby 1.8             ...
Native Threads
 • Green threads
1.8
       Ruby thread          native thread

 • Native (operation system) thread
1.9
   ...
GIL
                Giant Interpreter Lock


 •
1.9
                 C extension libraries
      thread safe
           Th...
4. Ruby on Rails
Ruby 1.9 on Rails

• Rails 2.3.2 or edge
• mysql-ruby driver for Ruby 1.9
• Passenger(mod_rails) or Thin
Upgrade issue
• program source encoding problem
 •   invalid multibyte char                                    # encoding:...
Benchmark

• Rails 2.3.2 production mode
• No DB, just render :text => 'hello'
• My MacBook Pro 2.2G
• httperf
Performance
              Reply rate [replies/s]

              min       avg            max   stddev
Ruby 1.8.6   241.8  ...
Performance
Ruby 1.9                            Rails Stack                   !!

 400

 300
                             ...
Performance
Ruby 1.9                            Rails Stack                   !!

 400

 300
                             ...
5. Conclusion
TIOBE Programming Community Index for April 2009
Ruby challenge
• Ruby 1.9 change a lot and 1.8 is slow and
  should be deprecated.
• Migrate to Ruby 1.9 is the big challe...
Reference
•   Understanding M17n
    http://blog.grayproductions.net/articles/understanding_m17n


•   Books
    •     Pro...
Other reference links
•   http://svn.ruby-lang.org/repos/ruby/tags/v1_9_1_0/NEWS

•   http://blog.nuclearsquid.com/writing...
Thank you.
Bonus
Object#tap
puts quot;dogquot;.reverse
          .tap{ |o| puts quot;reversed: #{o}quot; }
          .upcase

#
reversed: g...
Upcoming SlideShare
Loading in …5
×

Use Regexp to handle String Ruby 1.9

23,806 views

Published on

Use Regexp to handle String
Regexp



>> quot; quot;.scan(/./u).reverse.join
1.8,1.9

=> quot; quot;

>> quot; quot;.scan(/./u).size
=> 2

  • Ruby (Oprah's Book Club 2.0) --- http://amzn.to/1R7bSvB
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Sterling Silver Created Ruby and Created White Sapphire Wrapped Heart Pendant Necklace, 18" --- http://amzn.to/1TYeO01
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • The Ruby Programming Language --- http://amzn.to/1PkaYX4
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Good points :)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
курс пептидов на массу

http://220km.net

×