Ruby 1.8 和 Ruby 1.9 有什么区别 [英] What is the difference between Ruby 1.8 and Ruby 1.9
问题描述
我不清楚当前"版本的 Ruby (1.8) 和新"版本 (1.9) 之间的区别.是否有简单"或简单"的差异解释以及为什么如此不同?
I'm not clear on the differences between the "current" version of Ruby (1.8) and the "new" version (1.9). Is there an "easy" or a "simple" explanation of the differences and why it is so different?
推荐答案
Sam Ruby 有一个 很酷的幻灯片显示大纲差异.
Sam Ruby has a cool slideshow that outline the differences.
为了使这些信息内联以便于参考,并且如果链接在抽象的未来失效,这里是 Sam 幻灯片的概述.幻灯片放映不那么难以查看,但将其全部放在这样的列表中也很有帮助.
In the interest of bringing this information inline for easier reference, and in case the link goes dead in the abstract future, here's an overview of Sam's slides. The slideshow is less overwhelming to review, but having it all laid out in a list like this is also helpful.
- 性能
- 线/纤维
- 编码/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}
Action: Convert to {1 => 2}
红宝石 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
红宝石 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
操作:使用分号、then 或换行符
Action: Use semicolon, then, or newline
红宝石 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
Hash.index
Deprecated
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
操作:使用Hash.key
红宝石 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
(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
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"}
订单是广告订单
红宝石 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
tr
and Regexp
Now Understand 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
和 unpack
红宝石 1.8.6
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
<小时>
BasicObject
比 BlankSlate
更残酷红宝石 1.9
BasicObject
More Brutal Than 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
行动:使用::Math::PI
Action: Use ::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>
红宝石 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
instance_methods
Now an Array of Symbols
Ruby 1.9
irb(main):001:0> {}.methods.sort.last
=> :zip
红宝石 1.8.6
irb(main):001:0> {}.methods.sort.last
=> "zip"
操作:替换 instance_methods.include?用method_defined?
Action: Replace instance_methods.include? with method_defined?
# 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.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
to_enum
Ruby 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
<小时>
正则表达式属性"
红宝石 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
<小时>
HTH!
HTH!
这篇关于Ruby 1.8 和 Ruby 1.9 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!