题
我不明确之间的差异"前的"版本的红宝石(1.8)和"新"的版本(1.9).是有一个"简易"或"简单"的解释的差异和为什么这么不同?
解决方案
萨姆红宝石有一个 酷幻灯片,概述差异.
在感兴趣的,将这一信息内联,便于参考,并在案件的链接死在的抽象的未来,这里概述了萨姆的幻灯片。幻灯片的小压倒性的审查,但是具有它所有的布局在一个列表中的这样也是有帮助的。
Ruby1.9-主要特点
- 性能
- 线/纤维
- 编码/Unicode
- 宝石(主要)建立在现在
- 如果声明不会引入范围中的红宝石。
什么改变了?
单个字符串。
Ruby1.9
irb(main):001:0> ?c
=> "c"
红宝石的1.8.6
irb(main):001:0> ?c
=> 99
串的指数。
Ruby1.9
irb(main):001:0> "cat"[1]
=> "a"
红宝石的1.8.6
irb(main):001:0> "cat"[1]
=> 97
{的"a"、"b"}不再支持
Ruby1.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}
行动: 转换到{1=>2}
Array.to_s
现在包含的标点符号
Ruby1.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"
行动: 使用。加入而不是
结肠不再有效时的发言
Ruby1.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
行动: 使用的分号,然后,或newline
块变量现在阴影的地方变量
Ruby1.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
弃用
Ruby1.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
行动: 使用的散列。关键
Fixnum.to_sym
现在已经没有了
Ruby1.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
(Cont'd)红宝石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
哈希键,现在无序
Ruby1.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经常表达式
Ruby1.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
和 Regexp
现在明白了Unicode
Ruby1.9
unicode(string).tr(CP1252_DIFFERENCES, UNICODE_EQUIVALENT).
gsub(INVALID_XML_CHAR, REPLACEMENT_CHAR).
gsub(XML_PREDEFINED) {|c| PREDEFINED[c.ord]}
pack
和 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
Ruby1.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
行动: 使用::数学::PI
代表团的变化
Ruby1.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>
使用美元KCODE产生的警告
Ruby1.9
irb(main):004:1> $KCODE = 'UTF8'
(irb):4: warning: variable $KCODE is no longer effective; ignored
=> "UTF8"
红宝石的1.8.6
irb(main):001:0> $KCODE = 'UTF8'
=> "UTF8"
instance_methods
现在一系列符号
Ruby1.9
irb(main):001:0> {}.methods.sort.last
=> :zip
红宝石的1.8.6
irb(main):001:0> {}.methods.sort.last
=> "zip"
行动: 替换instance_methods.包括?与method_defined?
来源文件的编码
基本的
# coding: utf-8
Emacs
# -*- encoding: utf-8 -*-
家当
#!/usr/local/rubybook/bin/ruby
# encoding: utf-8
真正的线程
- 竞争条件
- 隐含的排序的假设
- 测试码
有什么新的吗?
替代语法符号作为杂键
Ruby1.9
{a: b}
redirect_to action: show
红宝石的1.8.6
{:a => b}
redirect_to :action => show
块地方变量
Ruby1.9
[1,2].each {|value; t| t=value*value}
注射方法
Ruby1.9
[1,2].inject(:+)
红宝石的1.8.6
[1,2].inject {|a,b| a+b}
to_enum
Ruby1.9
short_enum = [1, 2, 3].to_enum
long_enum = ('a'..'z').to_enum
loop do
puts "#{short_enum.next} #{long_enum.next}"
end
没有块?Enum!
Ruby1.9
e = [1,2,3].each
Lambda的简写
Ruby1.9
p = -> a,b,c {a+b+c}
puts p.(1,2,3)
puts p[1,2,3]
红宝石的1.8.6
p = lambda {|a,b,c| a+b+c}
puts p.call(1,2,3)
复杂的数字
Ruby1.9
Complex(3,4) == 3 + 4.im
小数仍然不是默认的
Ruby1.9
irb(main):001:0> 1.2-1.1
=> 0.0999999999999999
Regex"属性"
Ruby1.9
/\p{Space}/
红宝石的1.8.6
/[:space:]/
啪在中间
Ruby1.9
def foo(first, *middle, last)
(->a, *b, c {p a-c}).(*5.downto(1))
纤维
Ruby1.9
f = Fiber.new do
a,b = 0,1
Fiber.yield a
Fiber.yield b
loop do
a,b = b,a+b
Fiber.yield b
end
end
10.times {puts f.resume}
打破值
Ruby1.9
match =
while line = gets
next if line =~ /^#/
break line if line.find('ruby')
end
"嵌套的"方法
Ruby1.9
def toggle
def toggle
"subsequent times"
end
"first time"
end
禾田!
不隶属于 StackOverflow