level 9
sevk
楼主
改进:
执行效率
Threads/Fibers
Encoding/Unicode
gems is (mostly) built-in now
Single character strings.
12345678 Ruby 1.9 irb(main):001:0> "cat"[1] => "a" Ruby 1.8.6 irb(main):001:0> "cat"[1] => 97
String index.
12345678 Ruby 1.9 irb(main):001:0> "cat"[1] => "a" Ruby 1.8.6 irb(main):001:0> "cat"[1] => 97
{"a","b"} 写法不再支持123456789 Ruby 1.9 irb(main):002:0> {1,2} SyntaxError: (irb):2: syntax error, unexpected ',', expecting tASSOC Ruby 1.8.6 irb(main):001:0> {1,2} => {1=>2} Action: Convert to {1 => 2}
Array.to_s 现在包含逗号了.
123456789 Ruby 1.9 irb(main):001:0>p [1,2,3] => "[1, 2, 3]" Ruby 1.8.6 irb(main):001:0>p [1,2,3] => "123" Action: Use [1,2,3].join instead
when 条件 :冒号不再支持
12345678910 Ruby 1.9 irb(main):001:0> case 'a'; when /\w/: puts 'word'; end SyntaxError: (irb):1: syntax error, unexpected ':', expecting keyword_then or ',' or ';' or '\n' Ruby 1.8.6 irb(main):001:0> case 'a'; when /\w/: puts 'word'; end word Action: 使用分号或换新行
语句块里的变量是局部变量。
12345678910 Ruby 1.9 irb(main):001:0> i=0; [1,2,3].each {|i|}; i => 0 irb(main):002:0> i=0; for i in [1,2,3]; end; i => 3 Ruby 1.8.6 irb(main):001:0> i=0; [1,2,3].each {|i|}; i => 3
Hash.index 方法过时了,会出警告
123456789101112 Ruby 1.9 irb(main):001:0> {1=>2}.index(2) (irb):18: warning: Hash#index is deprecated; use Hash#key => 1 irb(main):002:0> {1=>2}.key(2) => 1 Ruby 1.8.6 irb(main):001:0> {1=>2}.index(2) => 1 Action: 使用 Hash.key
Fixnum.to_sym 方法没了,会出Error
123456789101112131415 Ruby 1.9 irb(main):001:0> 5.to_sym NoMethodError: undefined method 'to_sym' for 5:Fixnum Ruby 1.8.6 irb(main):001:0> 5.to_sym => nil (Cont'd) Ruby 1.9 # Find an argument value by name or index. def [](index) lookup(index.to_sym) end svn.ruby-lang.org/repos/ruby/trunk/lib/rake.rb
Hash的.Keys 现在是无序的。
12345678 Ruby 1.9 irb(main):001:0> {:a=>"a", :c=>"c", :b=>"b"} => {:a=>"a", :c=>"c", :b=>"b"} Ruby 1.8.6 irb(main):001:0> {:a=>"a", :c=>"c", :b=>"b"} => {:a=>"a", :b=>"b", :c=>"c"} 有序 :a :b :c
更严格的Unicode正则表达式
1234567 Ruby 1.9 irb(main):001:0> /\x80/u SyntaxError: (irb):2: invalid multibyte escape: /\x80/ Ruby 1.8.6 irb(main):001:0> /\x80/u => /\x80/u
tr 和 Regexp Now 支持 Unicode
123456789101112
13141516171
8 Ruby 1.9 unicode(string).tr(CP1252_DIFFERENCES, UNICODE_EQUIVALENT). gsub(INVALID_XML_CHAR, REPLACEMENT_CHAR). gsub(XML_PREDEFINED) {|c| PREDEFINED[c.ord]} pack and unpack Ruby 1.8.6 def xchr(escape=true) n = XChar::CP1252[self] || self case n when *XChar::VALID XChar::PREDEFINED[n] or (n>128 ? n.chr : (escape ? "&##{n};" : [n].pack('U*'))) else Builder::XChar::REPLACEMENT_CHAR end end unpack('U*').map {|n| n.xchr(escape)}.join