山姆·鲁比有一个概述差异的酷幻灯片.
为了将这些信息内联起来以便于参考,并且以防万一链接在抽象的未来失效,这里是 Sam 幻灯片的概述。幻灯片放映不太容易查看,但将所有内容都排列在这样的列表中也很有帮助。
Ruby 1.9 - 主要特性
- 表现
- 线/纤维
- 编码/统一码
- gems(大部分)现在是内置的
- if 语句不会在 Ruby 中引入作用域。
发生了什么变化?
单字符串。
Ruby 1.9
irb(main):001:0> ?c
=> "c"
红宝石 1.8.6
irb(main):001:0> ?c
=> 99
字符串索引。
Ruby 1.9
irb(main):001:0> "cat"[1]
=> "a"
红宝石 1.8.6
irb(main):001:0> "cat"[1]
=> 97
不再支持 {"a","b"}
Ruby 1.9
irb(main):002:0> {1,2}
SyntaxError: (irb):2: syntax error, unexpected ',', expecting tASSOC
红宝石 1.8.6
irb(main):001:0> {1,2}
=> {1=>2}
Action:转换为 {1 => 2}
Array.to_s
现在包含标点符号
Ruby 1.9
irb(main):001:0> [1,2,3].to_s
=> "[1, 2, 3]"
红宝石 1.8.6
irb(main):001:0> [1,2,3].to_s
=> "123"
Action:使用 .join 代替
When 语句中冒号不再有效
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'
红宝石 1.8.6
irb(main):001:0> case 'a'; when /\w/: puts 'word'; end
word
Action:使用分号、then 或换行符
块变量现在隐藏局部变量
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
红宝石 1.8.6
irb(main):001:0> i=0; [1,2,3].each {|i|}; i
=> 3
Hash.index
已弃用
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
红宝石 1.8.6
irb(main):001:0> {1=>2}.index(2)
=> 1
Action:使用Hash.key
Fixnum.to_sym
现在已经走了
Ruby 1.9
irb(main):001:0> 5.to_sym
NoMethodError: undefined method 'to_sym' for 5:Fixnum
红宝石 1.8.6
irb(main):001:0> 5.to_sym
=> nil
(续)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
哈希键现在无序
Ruby 1.9
irb(main):001:0> {:a=>"a", :c=>"c", :b=>"b"}
=> {:a=>"a", :c=>"c", :b=>"b"}
红宝石 1.8.6
irb(main):001:0> {:a=>"a", :c=>"c", :b=>"b"}
=> {:a=>"a", :b=>"b", :c=>"c"}
订单为插入顺序
更严格的 Unicode 正则表达式
Ruby 1.9
irb(main):001:0> /\x80/u
SyntaxError: (irb):2: invalid multibyte escape: /\x80/
红宝石 1.8.6
irb(main):001:0> /\x80/u
=> /\x80/u
tr
and Regexp
现在了解 Unicode
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
红宝石 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
BasicObject
比更残酷BlankSlate
Ruby 1.9
irb(main):001:0> class C < BasicObject; def f; Math::PI; end; end; C.new.f
NameError: uninitialized constant C::Math
红宝石 1.8.6
irb(main):001:0> require 'blankslate'
=> true
irb(main):002:0> class C < BlankSlate; def f; Math::PI; end; end; C.new.f
=> 3.14159265358979
Action:使用::Math::PI
授权变更
Ruby 1.9
irb(main):002:0> class C < SimpleDelegator; end
=> nil
irb(main):003:0> C.new('').class
=> String
红宝石 1.8.6
irb(main):002:0> class C < SimpleDelegator; end
=> nil
irb(main):003:0> C.new('').class
=> C
irb(main):004:0>
缺陷17700