Sam Ruby有
cool slideshow that outline the differences
.
为了让这些信息更容易被引用,并且万一链接在抽象的未来失效,下面是山姆幻灯片的概述。幻灯片放映不太容易被审查,但是把它全部放在这样的列表中也很有帮助。
Ruby 1.9-主要功能
-
性能
-
螺纹/纤维
-
编码/unicode
-
宝石现在(大部分)是内置的
-
if语句不在Ruby中引入范围。
发生了什么变化?
单个字符串。
红宝石1.9
irb(main):001:0> ?c
=> "c"
红宝石1.8
irb(main):001:0> ?c
=> 99
字符串索引。
红宝石1.9
irb(main):001:0> "cat"[1]
=> "a"
红宝石1.8
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
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
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
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
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
irb(main):001:0> {1=>2}.index(2)
=> 1
行动:
使用哈希键
Fixnum.to_sym
现在走了
红宝石1.9
irb(main):001:0> 5.to_sym
NoMethodError: undefined method 'to_sym' for 5:Fixnum
红宝石1.8
irb(main):001:0> 5.to_sym
=> nil
(继续)红宝石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
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
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
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
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
授权更改
红宝石1.9
irb(main):002:0> class C < SimpleDelegator; end
=> nil
irb(main):003:0> C.new('').class
=> String
红宝石1.8
irb(main):002:0> class C < SimpleDelegator; end
=> nil
irb(main):003:0> C.new('').class
=> C
irb(main):004:0>
Defect 17700
使用$kcode会产生警告
红宝石1.9
irb(main):004:1> $KCODE = 'UTF8'
(irb):4: warning: variable $KCODE is no longer effective; ignored
=> "UTF8"
红宝石1.8
irb(main):001:0> $KCODE = 'UTF8'
=> "UTF8"
instance_methods
现在是一组符号
红宝石1.9
irb(main):001:0> {}.methods.sort.last
=> :zip
红宝石1.8
irb(main):001:0> {}.methods.sort.last
=> "zip"
行动:
替换实例\u methods.include?用方法定义?
源文件编码
基本的
# coding: utf-8
Emacs
# -*- encoding: utf-8 -*-
谢邦
#!/usr/local/rubybook/bin/ruby
# encoding: utf-8
真实线程
有什么新鲜事吗?
符号作为哈希键的替代语法
红宝石1.9
{a: b}
redirect_to action: show
红宝石1.8
{:a => b}
redirect_to :action => show
块局部变量
红宝石1.9
[1,2].each {|value; t| t=value*value}
注入方法
红宝石1.9
[1,2].inject(:+)
红宝石1.8
[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
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
regex属性
红宝石1.9
/\p{Space}/
红宝石1.8
/[:space:]/
Middle的分裂
红宝石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
嗯!