我不清楚 Ruby 的“当前”版本(1.8)和“新”版本(1.9)之间的区别。是否有对差异的“简单”或“简单”解释以及为什么如此不同?
4 回答
Sam Ruby 有一个很酷的幻灯片,概述了不同之处。
为了将这些信息内联以便于参考,并且如果链接在抽象的未来失效,这里是 Sam 幻灯片的概述。幻灯片不那么难以查看,但是将所有内容都列在这样的列表中也很有帮助。
Ruby 1.9 - 主要特性
- 表现
- 线/纤维
- 编码/Unicode
- gems 现在(大部分)是内置的
- if 语句不会在 Ruby 中引入范围。
有什么改变?
单个字符串。
红宝石 1.9
irb(main):001:0> ?c
=> "c"
红宝石 1.8.6
irb(main):001:0> ?c
=> 99
字符串索引。
红宝石 1.9
irb(main):001:0> "cat"[1]
=> "a"
红宝石 1.8.6
irb(main):001:0> "cat"[1]
=> 97
{"a","b"} 不再受支持
红宝石 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}
行动:转换为 {1 => 2}
Array.to_s
现在包含标点符号
红宝石 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"
行动:使用 .join 代替
冒号在 When 语句中不再有效
红宝石 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
行动:使用分号,然后,或换行
块变量现在隐藏局部变量
红宝石 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
已弃用
红宝石 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
行动:使用 Hash.key
Fixnum.to_sym
现在走了
红宝石 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
哈希键现在无序
红宝石 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 正则表达式
红宝石 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
现在Regexp
了解 Unicode
红宝石 1.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
红宝石 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
行动:使用 ::Math::PI
授权变更
红宝石 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>
使用 $KCODE 会产生警告
红宝石 1.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
现在是符号数组
红宝石 1.9
irb(main):001:0> {}.methods.sort.last
=> :zip
红宝石 1.8.6
irb(main):001:0> {}.methods.sort.last
=> "zip"
行动:替换instance_methods.include?用方法定义?
源文件编码
基本的
# coding: utf-8
Emacs
# -*- encoding: utf-8 -*-
社邦
#!/usr/local/rubybook/bin/ruby
# encoding: utf-8
真正的线程
- 比赛条件
- 隐式排序假设
- 测试代码
什么是新的?
Symbol as Hash Keys 的替代语法
红宝石 1.9
{a: b}
redirect_to action: show
红宝石 1.8.6
{:a => b}
redirect_to :action => show
块局部变量
红宝石 1.9
[1,2].each {|value; t| t=value*value}
注入方法
红宝石 1.9
[1,2].inject(:+)
红宝石 1.8.6
[1,2].inject {|a,b| a+b}
to_enum
红宝石 1.9
short_enum = [1, 2, 3].to_enum
long_enum = ('a'..'z').to_enum
loop do
puts "#{short_enum.next} #{long_enum.next}"
end
没有块?枚举!
红宝石 1.9
e = [1,2,3].each
Lambda 速记
红宝石 1.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)
复数
红宝石 1.9
Complex(3,4) == 3 + 4.im
十进制仍然不是默认值
红宝石 1.9
irb(main):001:0> 1.2-1.1
=> 0.0999999999999999
正则表达式“属性”</h2>
红宝石 1.9
/\p{Space}/
红宝石 1.8.6
/[:space:]/
中间喷
红宝石 1.9
def foo(first, *middle, last)
(->a, *b, c {p a-c}).(*5.downto(1))
纤维
红宝石 1.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}
中断值
红宝石 1.9
match =
while line = gets
next if line =~ /^#/
break line if line.find('ruby')
end
“嵌套”方法
红宝石 1.9
def toggle
def toggle
"subsequent times"
end
"first time"
end
!
一个巨大的区别是从 Matz 的解释器转移到YARV,这是一种对性能有显着帮助的字节码虚拟机。
现在许多人推荐使用The Ruby Programming Language而不是 Pickaxe - 更重要的是,它包含 1.8/1.9 差异的所有细节。
还有一些变化:
返回一个 splat 单例数组:
def function
return *[1]
end
a=function
- 红宝石 1.9:[1]
- 红宝石 1.8 : 1
数组参数
def function(array)
array.each { |v| p v }
end
function "1"
- 红宝石 1.8:“1”
- ruby 1.9:“1”的未定义方法“每个”:字符串