ruby1.8到1.9 的改变 (不到20项)
ruby吧
全部回复
仅看楼主
level 9
sevk 楼主

改进:
执行效率
Threads/Fibers
Encoding/Unicode
gems is (mostly) built-in now
Single character strings.
12345678 Ruby 1.9 irb(main):001:0> "cat"[1] => "a" Ruby 1.8.6 irb(main):001:0> "cat"[1] => 97
String index.
12345678 Ruby 1.9 irb(main):001:0> "cat"[1] => "a" Ruby 1.8.6 irb(main):001:0> "cat"[1] => 97
{"a","b"} 写法不再支持123456789 Ruby 1.9 irb(main):002:0> {1,2} SyntaxError: (irb):2: syntax error, unexpected ',', expecting tASSOC Ruby 1.8.6 irb(main):001:0> {1,2} => {1=>2} Action: Convert to {1 => 2}
Array.to_s 现在包含逗号了.
123456789 Ruby 1.9 irb(main):001:0>p [1,2,3] => "[1, 2, 3]" Ruby 1.8.6 irb(main):001:0>p [1,2,3] => "123" Action: Use [1,2,3].join instead
when 条件 :冒号不再支持
12345678910 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' Ruby 1.8.6 irb(main):001:0> case 'a'; when /\w/: puts 'word'; end word Action: 使用分号或换新行
语句块里的变量是局部变量。
12345678910 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 Ruby 1.8.6 irb(main):001:0> i=0; [1,2,3].each {|i|}; i => 3
Hash.index 方法过时了,会出警告
123456789101112 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 Ruby 1.8.6 irb(main):001:0> {1=>2}.index(2) => 1 Action: 使用 Hash.key
Fixnum.to_sym 方法没了,会出Error
123456789101112131415 Ruby 1.9 irb(main):001:0> 5.to_sym NoMethodError: undefined method 'to_sym' for 5:Fixnum Ruby 1.8.6 irb(main):001:0> 5.to_sym => nil (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
Hash的.Keys 现在是无序的。
12345678 Ruby 1.9 irb(main):001:0> {:a=>"a", :c=>"c", :b=>"b"} => {:a=>"a", :c=>"c", :b=>"b"} Ruby 1.8.6 irb(main):001:0> {:a=>"a", :c=>"c", :b=>"b"} => {:a=>"a", :b=>"b", :c=>"c"} 有序 :a :b :c
更严格的Unicode正则表达式
1234567 Ruby 1.9 irb(main):001:0> /\x80/u SyntaxError: (irb):2: invalid multibyte escape: /\x80/ Ruby 1.8.6 irb(main):001:0> /\x80/u => /\x80/u
tr 和 Regexp Now 支持 Unicode
123456789101112
13141516171
8 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 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
2013年01月20日 04点01分 1
level 9
sevk 楼主

BasicObject 比 BlankSlate 惨无人道
12345678910 Ruby 1.9 irb(main):001:0> class C < BasicObject; def f; Math::PI; end; end; C.new.f NameError: uninitialized constant C::Math Ruby 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.14
15926535897
9 Action: Use ::Math::PI
Delegation 改变
12345678910111213 Ruby 1.9 irb(main):002:0> class C < SimpleDelegator; end => nil irb(main):003:0> C.new('').class => String Ruby 1.8.6 irb(main):002:0> class C < SimpleDelegator; end => nil irb(main):003:0> C.new('').class => C irb(main):004:0>
Defect 17700
使用 $KCODE 会出警告
12345678 Ruby 1.9 irb(main):004:1> $KCODE = 'UTF8' (irb):4: warning: variable $KCODE is no longer effective; ignored => "UTF8" Ruby 1.8.6 irb(main):001:0> $KCODE = 'UTF8' => "UTF8"
内置方法名改成Symble,以前是String
12345678 Ruby 1.9 irb(main):001:0> {}.methods.sort.last => :zip Ruby 1.8.6 irb(main):001:0> {}.methods.sort.last => "zip" Action: 用 method_defined?替代instance_methods.include?
源文件编码
123456789 Basic
# coding: utf-8 Emacs #
-*- encoding: utf-8 -*- Shebang #!/usr/local/rubybook/bin/ruby # encoding: utf-8
系统级别的真正线程,1.8是ruby自己模拟的线程
123 Race Conditions Implicit Ordering Assumptions Test Code
新特性
Hash的Keys支持符号
12345678910 Ruby 1.9 {a: b} redirect_to action: show Ruby 1.8.6 {:a => b} redirect_to :action => show
代码块内局部变量
123456789101112
13141516171
81920 Ruby 1.9 [1,2].each {|value; t| t=value*value} Inject Methods Ruby 1.9 [1,2].inject(:+) Ruby 1.8.6 [1,2].inject {|a,b| a+b} 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
可以省略{}块
123 Ruby 1.9 e = [1,2,3].each
Lambda 简写
12345678 Ruby 1.9 p = -> a,b,c {a+b+c} puts p.(1,2,3) puts p[1,2,3] Ruby 1.8.6 p = lambda {|a,b,c| a+b+c} puts p.call(1,2,3)
复数
123456789101112
13141516171
8192021222324 Ruby 1.9 Complex(3,4) == 3 + 4.im Decimal Is Still Not The Default Ruby 1.9 irb(main):001:0> 1.2-1.1 => 0.0999999999999999 Regex “Properties” Ruby 1.9 /\p{Space}/ Ruby 1.8.6 /[:space:]/ Splat in Middle Ruby 1.9 def foo(first, *middle, last) (->a, *b, c {p a-c}).(*5.downto(1))
Fibers
123456789101112 Ruby 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}
Break 可以有参数
1234567 Ruby 1.9 match = while line = gets next if line =~ /^#/ break line if line.find('ruby') end
def嵌套def
12345678 Ruby 1.9 def toggle def toggle "subsequent times" end "first time" end
原文:
http://stackoverflow.com/questions/21574/what-is-the-difference-between-ruby-1-8-and-ruby-1-9
2013年01月20日 04点01分 2
level 10
顶一个.
2013年01月24日 11点01分 3
1