?
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
ruby version 1.7是開(kāi)發(fā)版。將來(lái)可能會(huì)刪除下列中的部分內(nèi)容,也可能因?yàn)榧嫒菪詥?wèn)題而對(duì)其進(jìn)行修改。
新增。作為profile.rb的實(shí)體將其分離出來(lái)。
新增。用在allocate方法的定義中。 [ruby-dev:19116]
返回布爾值。
p(/foo/ === "foo") => ruby 1.6.8 (2002-12-24) [i586-linux] 0 => ruby 1.8.0 (2003-03-12) [i586-linux] true
遇到對(duì)屬性賦值或?qū)?shù)組元素進(jìn)行賦值的情況時(shí),返回"assignment"而非"method"。
class Foo attr_accessor :foo end p defined? Foo.new.foo = 1 ary = [] p defined? ary[2] = 1 => ruby 1.6.8 (2002-12-24) [i586-linux] "method" "method" => ruby 1.8.0 (2003-03-12) [i586-linux] "assignment" "assignment"
新增
加入了WindowsCE的支持補(bǔ)丁。
向IO#read, IO#sysread新增了第二參數(shù)(指定了預(yù)先分配好的讀入緩沖)
新增。與Thread#kill 相同。
新增。與abort, exit函數(shù)相同。
新增
改名了,原名為become。(此后,在1.8中又改名為initialize_copy)
增加了參數(shù)。
ruby -e 'raise SystemExit.new(2)' echo $? # => 2
新增
p [[1,2,3], [4,5,6], [7,8,9]].transpose => ruby 1.7.3 (2002-12-11) [i586-linux] [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
試驗(yàn)性的修改。
a = 1 p a / 5 => ruby 1.6.8 (2002-12-24) [i586-linux] 0 => ruby 1.8.0 (2003-03-12) [i586-linux] 0 a = 1 p a /5 => -:2: warning: ambiguous first argument; make sure -:2: unterminated regexp meets end of file ruby 1.6.8 (2002-12-24) [i586-linux] => ruby 1.8.0 (2003-03-12) [i586-linux] 0
新增 (Object#id是obsolete)
p Object.new.id => ruby 1.6.7 (2002-03-01) [i586-linux] 537730140 => -:1: warning: Object#id will be deprecated; use Object#object_id ruby 1.7.3 (2002-12-04) [i586-linux] 537723790
新增(取消了Symbol#intern)
新增
p [1,2,3].zip([4,5,6], [7,8,9]) => ruby 1.7.3 (2002-12-11) [i586-linux] [[1, 4, 7], [2, 5, 8], [3, 6, 9]] p [1,2,3].zip([4,5,6], [7,8,9]) {|v| p v} => ruby 1.7.3 (2002-12-11) [i586-linux] [1, 4, 7] [2, 5, 8] [3, 6, 9] nil
[ruby-dev:18606]
增加了Module#private_method_defined?,Module#protected_method_defined?,Module#public_method_defined?
修改了Object#methods, Module#instance_methods(為了與 Module#method_defined?和Module#instance_methods的關(guān)系 取得一致)
class Foo def public_m; end private def private_m; end protected def protected_m; end end foo = Foo.new m = %w(public_m private_m protected_m) p m.collect {|_| Foo.method_defined?(_)} if Foo.respond_to? :public_method_defined? p m.collect {|_| Foo.public_method_defined?(_)} p m.collect {|_| Foo.private_method_defined?(_)} p m.collect {|_| Foo.protected_method_defined?(_)} end puts '---' p m.collect {|_| Foo.instance_methods.member?(_)} p m.collect {|_| Foo.public_instance_methods.member?(_)} p m.collect {|_| Foo.private_instance_methods.member?(_)} p m.collect {|_| Foo.protected_instance_methods.member?(_)} puts '---' p m.collect {|_| foo.methods.member?(_)} p m.collect {|_| foo.public_methods.member?(_)} p m.collect {|_| foo.private_methods.member?(_)} p m.collect {|_| foo.protected_methods.member?(_)} => ruby 1.6.8 (2002-12-24) [i586-linux] [true, false, true] --- [true, false, false] [true, false, false] [false, true, false] [false, false, true] --- [true, false, false] [true, false, false] [false, true, false] [false, false, true] => ruby 1.8.0 (2003-03-09) [i586-linux] [true, false, true] [true, false, false] [false, true, false] [false, false, true] --- [true, false, true] [true, false, false] [false, true, false] [false, false, true] --- [true, false, true] [true, false, false] [false, true, false] [false, false, true]
采用了符號(hào)的擴(kuò)展表示法。[ruby-dev:18537]
p :"foo#{"bar"}" p :'foo#{"bar"}' p %s{foo#{"bar"}} => ruby 1.7.3 (2002-11-14) [i586-linux] :foobar :"foo\#{\"bar\"}" :"foo\#{\"bar\"}"
修改了rescue修飾部分的優(yōu)先級(jí)。好像是試驗(yàn)性的修改。 (在1.8版本中正式采用了這個(gè)修改)。因此
a = b rescue c
不會(huì)被解釋成
(a = b) rescue c
而是被解釋為
a = (b rescue c)
雖然與if修飾部分的優(yōu)先級(jí)有所不同,但它有個(gè)好處:如果b發(fā)生異常時(shí)可以使用c的值。
# 若在以前的版本(1.6)中執(zhí)行下列代碼時(shí),則不會(huì)進(jìn)行賦值 # 只是對(duì)變量進(jìn)行了定義,結(jié)果是v等于nil。 v = raise rescue true p v => ruby 1.6.7 (2002-03-01) [i586-linux] nil => ruby 1.7.3 (2002-10-18) [i586-linux] true
使用它就會(huì)出現(xiàn)警告。請(qǐng)您使用Object#class來(lái)代替它。
p Object.new.type => -:1: warning: Object#type is deprecated; use Object#class ruby 1.7.3 (2002-10-08) [i586-linux] Object
在類定義表達(dá)式的末尾才會(huì)調(diào)用inherited方法。 [ruby-bugs-ja:PR#342]
def Object.inherited(c) p "inherited!" end class Foo p "defining Foo" end => ruby 1.6.7 (2002-03-01) [i586-linux] "inherited!" "defining Foo" => ruby 1.7.3 (2002-10-04) [i586-linux] "defining Foo" "inherited!"
若在方法定義的外側(cè)調(diào)用return的話,則會(huì)在運(yùn)行時(shí)而非編譯時(shí)引發(fā)錯(cuò)誤。
p :doing return => -:2: return appeared outside of method ruby 1.6.7 (2002-03-01) [i586-linux] => ruby 1.7.3 (2002-10-04) [i586-linux] :doing -:2: unexpected return
以前,使用||=對(duì)未定義的變量進(jìn)行賦值時(shí),會(huì)在全局變量中出現(xiàn)警告。另外,在類變量中會(huì)引發(fā)錯(cuò)誤。 [ruby-dev:18278]
local ||= 1 @instance ||= 1 $global ||= 1 @@class ||= 1 => -:3: warning: global variable `$global' not initialized -:4: uninitialized class variable @@class in Object (NameError) ruby 1.6.7 (2002-03-01) [i586-linux] => ruby 1.7.3 (2002-09-13) [i586-linux]
在mswin32版和mingw32版中,ruby會(huì)在內(nèi)部將進(jìn)程ID變?yōu)檎龜?shù)。雖然在NT系列的OS中沒(méi)有什么變化,但在Win9x系列的OS中,由OS控制的進(jìn)程ID是負(fù)數(shù),所以才將其變?yōu)檎龜?shù)。[ruby-dev:18263]
在對(duì)File::NONBLOCK模式的IO進(jìn)行讀入操作時(shí),如果發(fā)生EWOULDBLOCK的話,可能會(huì)導(dǎo)致讀入數(shù)據(jù)丟失。 [ruby-dev:17855]
在使用Thread的程序中,如果從文件中讀出數(shù)據(jù)并寫(xiě)入socket時(shí),可能會(huì)在Socket#write中引發(fā)Errno::EINTR,但這種情況極少出現(xiàn)。[ruby-dev:17878], [ruby-core:00444]
無(wú)法對(duì)包含(include)了無(wú)名模塊的對(duì)象進(jìn)行dump。 [ruby-dev:18186]
class << obj = Object.new include Module.new end Marshal.dump(obj) => ruby 1.6.7 (2002-03-01) [i586-linux] => -:4:in `dump': can't dump anonymous class #<Module:0x401a871c> (ArgumentError) from -:4 ruby 1.7.3 (2002-09-06) [i586-linux]
可以dump包含(include)了有名模塊的對(duì)象,此時(shí)模塊的信息被保存到dump format之中。
module M def foo p :foo end end class << obj = Object.new include M end p dump = Marshal.dump(obj) p obj2 = Marshal.load(dump) class << obj2 p included_modules end obj2.foo => ruby 1.6.7 (2002-03-01) [i586-linux] "\004\006o:\vObject\000" #<Object:0x401a9630> [Kernel] -:14: undefined method `foo' for #<Object:0x401a9630> (NameError) => ruby 1.7.3 (2002-09-06) [i586-linux] "\004\ae:\006Mo:\vObject\000" #<Object:0x401a821c> [M, Kernel] :foo
因此將format version由4.7提升到4.8。 (2002-09-17)
開(kāi)始著手合并extmk.rb和mkmf.rb。extmk.rb將會(huì)用到mkmf.rb。相應(yīng)地對(duì)mkmf.rb也作出調(diào)整。[ruby-dev:18109]
定義規(guī)定:類的特殊類的特殊類,就是特殊類本身[ruby-bugs-ja:PR#313]。不太明白(^^;
class << Object p [self.id, self] class << self p [self.id, self] end end => ruby 1.6.7 (2002-03-01) [i586-linux] [537771634, Class] [537742484, Class] => ruby 1.7.3 (2002-09-05) [i586-linux] [537771634, #<Class:Object>] [537771634, #<Class:Object>]
另外好像說(shuō),對(duì)象的特殊類的超類的特殊類 和 對(duì)象的特殊類的特殊類的超類是一回事兒[ruby-bugs-ja:PR#324]。更不明白了(^^;;
class << Object.new class << self.superclass p [self.id, self] end class << self p [self.superclass.id, self.superclass] end end => ruby 1.6.7 (2002-03-01) [i586-linux] [537771634, Class] [537771644, Class] => ruby 1.7.3 (2002-09-05) [i586-linux] [537771634, #<Class:Object>] [537771634, #<Class:Object>]
[ruby-bugs-ja:PR#336]中好像還有些變化 (請(qǐng)參考2002-09-21的ChangeLog。)
新增
新增(后來(lái)改名為copy_object。再后來(lái)又改名為initialize_copy)
ary = [1,2,3] p ary, ary.id ary.become [3,2,1] p ary, ary.id => ruby 1.7.3 (2002-08-30) [i586-linux] [1, 2, 3] 537743354 [3, 2, 1] 537743354 ary = [1,2,3] p ary, ary.id ary.replace [3,2,1] p ary, ary.id => ruby 1.7.3 (2002-08-30) [i586-linux] [1, 2, 3] 537743354 [3, 2, 1] 537743354 obj = Object.new p obj, obj.id obj.become Object.new p obj, obj.id => ruby 1.7.3 (2002-08-30) [i586-linux] #<Object:0x401a9ff4> 537743354 #<Object:0x401a9ff4> 537743354
保證了mswin32版ruby 和 MinGW版ruby中的擴(kuò)展庫(kù)的兼容性。分別把Config::CONFIG['RUBY_SO_NAME']變更為msvcrt-rubyXX(成為DLL名),把Config::CONFIG['sitearch'](擴(kuò)展庫(kù)所在地的路徑元素)變更為"i386-msvcrt"。 [ruby-dev:17144], [ruby-dev:18047]
在這次修改中,新增了sitearch(在其他環(huán)境中,則與CONFIG['arch']相同)
另外請(qǐng)參考Win32 native版的腳注
在各輸出方法中,只有putc不使用write方法。 [ruby-dev:18038]
class << foo = STDOUT.dup def write(s) p "foo" end end foo.putc("bar") puts => ruby 1.6.7 (2002-03-01) [i586-linux] b => ruby 1.7.3 (2002-09-05) [i586-linux] "foo"
新增 [ruby-dev:17966]
在Proc#to_s 的結(jié)果中新增了腳本的源文件名和行號(hào)。[ruby-dev:17968]
p Proc.new { 2 3 }.to_s => -:2: warning: useless use of a literal in void context ruby 1.6.7 (2002-03-01) [i586-linux] "#<Proc:0x401ab8b8>" => -:2: warning: useless use of a literal in void context ruby 1.7.3 (2002-09-05) [i586-linux] "#<Proc:0x0x401a87d0@-:2>"
不能將字符串指定給參數(shù)了。
[1,2,3].find("p :nothing") {|v| v > 5} => ruby 1.6.7 (2002-03-01) [i586-linux] :nothing => -:1:in `find': undefined method `call' for "p :nothing":String (NoMethodError) from -:1 ruby 1.7.2 (2002-08-01) [i586-linux]
另外,若沒(méi)有找到元素的話,就返回ifnone 的結(jié)果。
p [1,2,3].find(proc {:nothing}) {|v| v > 5} => ruby 1.6.7 (2002-03-01) [i586-linux] nil => ruby 1.7.2 (2002-08-01) [i586-linux] :nothing
新增。
在生成隨機(jī)數(shù)的算法中使用了Mersenne Twister。
允許對(duì)方法定義進(jìn)行嵌套。
def func1 def func2 p :func2 end end func1 func2 => -:2: nested method definition ruby 1.6.7 (2002-03-01) [i586-linux] => ruby 1.7.3 (2002-09-05) [i586-linux] :func2
允許在方法定義中出現(xiàn)alias, undef。
def bar end def foo p :foo undef bar end foo def bar p :bar alias foo bar end bar foo => -:5: undef within method -:12: alias within method ruby 1.6.7 (2002-03-01) [i586-linux] => ruby 1.7.3 (2002-09-05) [i586-linux] :foo -:10: warning: method redefined; discarding old bar -:10: warning: overriding global function `bar' :bar :bar
在方法定義外側(cè)調(diào)用super時(shí),將會(huì)在運(yùn)行時(shí)而不是編譯時(shí)引發(fā)錯(cuò)誤。
p 1 super => -:2: super called outside of method ruby 1.6.7 (2002-03-01) [i586-linux] => ruby 1.7.3 (2002-09-05) [i586-linux] 1 -:2: super called outside of method (NoMethodError)
也許[ruby-dev:16969]中給出了變更的理由。[ruby-dev:17882]
新增了10進(jìn)制整數(shù)字面值的0d前綴。
p 0d10 => ruby 1.7.3 (2002-09-04) [i586-linux] 10 p 0d10.1 => -:1: parse error ruby 1.7.3 (2002-09-04) [i586-linux]
允許使用大寫(xiě)字母。
p 0D10 => ruby 1.7.3 (2002-09-04) [i586-linux] 10
但不能像下面這樣。
p(/\d10/) p "\d10" => ruby 1.7.3 (2002-09-04) [i586-linux] /\d10/ "d10"
只要字面值允許,Integer()也就允許。
p Integer("0d010") => ruby 1.7.3 (2002-09-04) [i586-linux] 10 p Integer("0d010.1") => -:1:in `Integer': invalid value for Integer: "0d010.1" (ArgumentError) from -:1 ruby 1.7.3 (2002-09-04) [i586-linux]
String#to_i、String#oct也是如此
p "0d010".to_i p "0d010".oct => ruby 1.6.7 (2002-03-01) [i586-linux] 0 0 => ruby 1.7.3 (2002-09-04) [i586-linux] 10 10
新增set_socket方法
新增了8進(jìn)制字面值的0和0o前綴。
p 0o377 => ruby 1.7.3 (2002-09-04) [i586-linux] 255
可以使用大寫(xiě)字母。
p 0O377 => ruby 1.7.3 (2002-09-04) [i586-linux] 255
不能像下面這樣。
p(/\o377/) p "\o377" => ruby 1.7.3 (2002-09-04) [i586-linux] /\o377/ "o377"
只要字面值允許,Integer()也沒(méi)問(wèn)題。
p Integer("0o377") => -:1:in `Integer': invalid value for Integer: "0o377" (ArgumentError) from -:1 ruby 1.6.7 (2002-03-01) [i586-linux] => ruby 1.7.3 (2002-09-04) [i586-linux] 255
String#to_i、String#oct也是如此
p "0o377".oct p "0o377".to_i(8) => ruby 1.6.7 (2002-03-01) [i586-linux] 0 -:2:in `to_i': wrong # of arguments(1 for 0) (ArgumentError) from -:2 => ruby 1.7.3 (2002-09-04) [i586-linux] 255 255
若將字符串而非正則表達(dá)式傳給pattern的話,就直接把它用作匹配模型,而不會(huì)將其編譯成正則表達(dá)式。(準(zhǔn)確地講,不會(huì)Regexp.compile(arg)這樣處理,而是Regexp.compile(Regexp.quote(arg))這樣)
只有str =~ arg中的arg是字符串時(shí),才會(huì)執(zhí)行str.index(arg),它等價(jià)于Regexp.compile(Regexp.quote(arg)) =~ str(所以沒(méi)有設(shè)定$~)。
p "aaaa*".scan("a*") => ruby 1.6.7 (2002-03-01) [i586-linux] ["aaaa", "", ""] => -:1: warning: string pattern instead of regexp; metacharacters no longer effective ruby 1.7.3 (2002-09-04) [i586-linux] ["a*"] p "aa*aa*aa*".split("a*") => ruby 1.6.7 (2002-03-01) [i586-linux] ["", "*", "*", "*"] => -:1: warning: string pattern instead of regexp; metacharacters no longer effective ruby 1.7.3 (2002-09-04) [i586-linux] ["a", "a", "a"] p "aa*".sub('a*', '') => ruby 1.6.7 (2002-03-01) [i586-linux] "*" => -:1: warning: string pattern instead of regexp; metacharacters no longer effective ruby 1.7.3 (2002-09-04) [i586-linux] "a" p "aa*aa*aa*aa*".gsub('a*', '') => ruby 1.6.7 (2002-03-01) [i586-linux] "****" => -:1: warning: string pattern instead of regexp; metacharacters no longer effective ruby 1.7.3 (2002-09-04) [i586-linux] "aaaa" $_ = "aa*" p ~"a*" => ruby 1.6.7 (2002-03-01) [i586-linux] 0 => ruby 1.7.3 (2002-09-04) [i586-linux] 1
getbinaryfile() 的第二參數(shù)(本地文件名)變?yōu)榭蛇x參數(shù)。新增get(), put(), binary(),binary = 方法
聽(tīng)說(shuō)加入了支持Win32用的雙向管道的補(bǔ)丁 [ruby-win32:185]
使用它的話會(huì)出現(xiàn)警告消息。(聽(tīng)說(shuō)已經(jīng)變成obsolete)
p Object.new.to_a => ruby 1.6.7 (2002-03-01) [i586-linux] [#<Object:0x401ab8b8>] => -:1: warning: default `to_a' will be obsolete ruby 1.7.3 (2002-09-02) [i586-linux] [#<Object:0x401a88ac>]
Array()的參數(shù)不再接受nil。 (在ruby 1.8.0 (2003-05-29)中,又開(kāi)始接受nil了)
p Array(nil) => ruby 1.6.7 (2002-03-01) [i586-linux] [] => -:1:in `Array': cannot convert nil into Array (TypeError) from -:1 ruby 1.7.3 (2002-09-02) [i586-linux] => ruby 1.8.0 (2003-05-29) [i586-linux] []
新增了%W(...) 數(shù)組字面值。與%w()不同的是,它可以使用反斜線表示法和展開(kāi)式。[ruby-dev:15988]
v = "b c" p %W(a #{v}d\se) => ruby 1.7.3 (2002-09-04) [i586-linux] ["a", "b cd e"]
在把數(shù)值或字符串以外的對(duì)象變換為整數(shù)時(shí),不再使用to_i,而是使用to_int進(jìn)行變換。
class << obj = Object.new def to_i() 0 end def to_int() 1 end end p Integer(obj) => ruby 1.6.7 (2002-03-01) [i586-linux] 0 => ruby 1.7.3 (2002-09-02) [i586-linux] 1
新增
p nil.to_f => -:1: undefined method `to_f' for nil (NameError) ruby 1.6.7 (2002-03-01) [i586-linux] => ruby 1.7.3 (2002-09-02) [i586-linux] 0.0
Float()的參數(shù)不再接受nil。
p Float(nil) => ruby 1.6.7 (2002-03-01) [i586-linux] 0.0 => -:1:in `Float': cannot convert nil into Float (TypeError) from -:1 ruby 1.7.3 (2002-09-02) [i586-linux]
在#{ ... }展開(kāi)式中,可以書(shū)寫(xiě)任何ruby程序,包括字符串分隔符在內(nèi)。雖然以前也是如此,但此次則明確了規(guī)則。也就是說(shuō),展開(kāi)式中的語(yǔ)法規(guī)則與外面相同。ruby程序會(huì)被正確解析。[ruby-dev:17422]
(1.6 版本中,曾出現(xiàn)過(guò)異常的舉動(dòng))
p "#{ "foo" }" => ruby 1.6.7 (2002-03-01) [i586-linux] "foo" => -:1: warning: bad substitution in string -:1: parse error p "#{ "foo" }" ^ ruby 1.6.7 (2002-08-21) [i586-linux] => ruby 1.6.8 (2002-12-24) [i586-linux] "foo" => ruby 1.7.3 (2002-09-02) [i586-linux] "foo"
不應(yīng)該對(duì)下列分隔符進(jìn)行轉(zhuǎn)義。
p "#{ \"foo\" }" => ruby 1.6.7 (2002-03-01) [i586-linux] "foo" => ruby 1.6.7 (2002-08-21) [i586-linux] "foo" => -:1: warning: escaped terminator '"' inside string interpolation ruby 1.7.3 (2002-09-02) [i586-linux] "foo"
請(qǐng)注意:展開(kāi)式中注釋并不是從 # 到 } ,而是從 # 到換行。
p "#{ "foo" # comment }" => ruby 1.6.7 (2002-03-01) [i586-linux] "foo" => -:1: parse error ruby 1.7.3 (2002-09-02) [i586-linux] p "#{ "foo" # comment }" => ruby 1.6.7 (2002-03-01) [i586-linux] "foo" => ruby 1.7.3 (2002-09-02) [i586-linux] "foo"
字符串字面值中的行首的 __END__ 不再被當(dāng)作腳本的結(jié)束標(biāo)志了。[ruby-dev:17513]
# p " #__END__ #" p eval(%Q(p "\n__END__\n")) => -:1: compile error (SyntaxError) (eval):1: unterminated string meets end of file ruby 1.6.7 (2002-03-01) [i586-linux] => ruby 1.7.3 (2002-09-02) [i586-linux] "\n__END__\n" nil
?空格、?換行、?TAB 等不再是字面值。若必須使用的話,可以寫(xiě)成 ?\s, ?\n, ?\t 。(請(qǐng)注意,下例中的前半部分使用了雙引號(hào)) [ruby-bugs-ja:PR#261], [ruby-dev:17446]
p eval("?\t") p eval("?\n") p eval("?\v") p eval("?\f") p eval("?\r") p eval("? ") => ruby 1.6.7 (2002-03-01) [i586-linux] 9 10 11 12 13 32 => -:1: compile error (SyntaxError) (eval):1: parse error ruby 1.7.3 (2002-09-02) [i586-linux] p eval('?\t') p eval('?\n') p eval('?\v') p eval('?\f') p eval('?\r') p eval('?\s') => ruby 1.6.7 (2002-03-01) [i586-linux] 9 10 11 12 13 32 => ruby 1.7.3 (2002-09-02) [i586-linux] 9 10 11 12 13 32
合并了支持在bcc中編譯ruby解釋器的補(bǔ)丁。
Range#max, Range#min, Range#include? 使用 <=> 方法進(jìn)行范圍計(jì)算。[ruby-list:35253], [ruby-dev:17228] (2003-03-18: min, max 變回原樣。[ruby-dev:19837])
Range#member? 使用 each 來(lái)遍歷所有元素并確認(rèn)是否有member。(與Enumerable#member?相同)
截止1.6,max, min, member? include? 是 Enumerable 的方法,=== 是 Range的方法。在1.7中,max, min, member?, include?, === 都是 Range 的方法,include? 成了 === 的別名。(在1.8中,max, min重新成為 Enumerable 的方法)
因?yàn)檫@些變動(dòng)導(dǎo)致下列不同。
p((0.1 .. 2.0).include?(1.1)) => ruby 1.6.7 (2002-03-01) [i586-linux] false => ruby 1.7.3 (2002-09-02) [i586-linux] true p((0.1 .. 2.0).member?(1.0)) => ruby 1.6.7 (2002-03-01) [i586-linux] true => -:1:in `member?': cannot iterate from Float (TypeError) from -:1 ruby 1.7.3 (2002-09-02) [i586-linux] p "b" < "ba" p(("a"..."bc").max) => ruby 1.6.7 (2002-03-01) [i586-linux] true "b" => ruby 1.7.3 (2002-09-05) [i586-linux] true "bc" => ruby 1.8.0 (2003-03-20) [i586-linux] true "b"
新增
Range#each 使用各元素的 succ 方法進(jìn)行迭代操作。
(1.0 .. 2.0).each {|v| p v} => ruby 1.6.7 (2002-03-01) [i586-linux] 1 2 => -:1:in `each': cannot iterate from Float (TypeError) from -:1 ruby 1.7.3 (2002-09-02) [i586-linux] class Float def succ self + 1.0 end end (1.0 .. 2.0).each {|v| p v} => ruby 1.7.3 (2002-09-02) [i586-linux] 1.0 2.0
該方法也被刪除。 [ruby-talk:64479], [ruby-talk:72133]
p(("a".."z").size) => ruby 1.6.7 (2002-03-01) [i586-linux] 26 => -:1: undefined method `size' for #<Range:0x401aa780> (NoMethodError) ruby 1.7.2 (2002-08-01) [i586-linux]
若想得到Range的元素?cái)?shù)量,必須這樣
p(("a".."z").to_a.size) => ruby 1.7.2 (2002-08-01) [i586-linux] 26
才行。
新增
2003-01-21: 該修改好像是變回原來(lái)的樣子
對(duì) -數(shù)值 的字面值的解釋發(fā)生了變化,-數(shù)值 總是被當(dāng)作一個(gè)字面值來(lái)處理。
例如,下面的表達(dá)式的結(jié)果就各不相同。
p -2**2 => -:1: warning: ambiguous first argument; make sure ruby 1.6.7 (2002-03-01) [i586-linux] -4 => -:1: warning: ambiguous first argument; make sure ruby 1.7.2 (2002-08-01) [i586-linux] 4 => -:1: warning: ambiguous first argument; make sure ruby 1.8.0 (2003-03-12) [i586-linux] -4
以前-2**2 被解釋成-(2**2)。這是由于操作符的優(yōu)先級(jí)不同所致 (真的如此嗎?)。在1.7中則被解釋成(-2)**2。另外,若在 - 和數(shù)值之間插入空格的話, - 會(huì)被當(dāng)作單項(xiàng)操作符(方法)。(這與以前相同)
p(- 2**2) => ruby 1.6.7 (2002-03-01) [i586-linux] -4 => ruby 1.7.2 (2002-08-01) [i586-linux] -4 => ruby 1.8.0 (2003-03-12) [i586-linux] -4 class Fixnum def -@ 1 end end p(- 2**2) => -:2: warning: discarding old -@ ruby 1.6.7 (2002-03-01) [i586-linux] 1 => -:2: warning: method redefined; discarding old -@ ruby 1.7.2 (2002-08-01) [i586-linux] 1 => -:2: warning: method redefined; discarding old -@ ruby 1.8.0 (2003-03-12) [i586-linux] 1
新增
在將字符串變?yōu)楦↑c(diǎn)數(shù)時(shí),不再依賴庫(kù)函數(shù) strtod(3)了。這樣,即使修改了庫(kù)的內(nèi)容,也不會(huì)影響到它的運(yùn)作。
p "0xa.a".to_f => ruby 1.6.7 (2002-03-01) [i586-linux] 10.625 => ruby 1.7.2 (2002-08-01) [i586-linux] 0.0
表示最大精度的格式由"%.10g"變?yōu)?%.16g"。(2003-03-20: 其后又變?yōu)?%.15g" [ruby-bugs-ja:PR#406])
p 1.0/3 p 99.6 => ruby 1.6.7 (2002-03-01) [i586-linux] 0.3333333333 99.6 => ruby 1.7.2 (2002-08-01) [i586-linux] 0.3333333333333333 99.59999999999999 => ruby 1.8.0 (2003-03-20) [i586-linux] 0.333333333333333 99.6
可以使用limit來(lái)指定等待線程的時(shí)間。
新增
在使用 & 來(lái)修飾方法的參數(shù)時(shí),若傳給參數(shù)的對(duì)象中包含to_proc 方法就調(diào)用它,并把結(jié)果當(dāng)作塊來(lái)傳給方法。以前,& 就只能修飾Proc, Method對(duì)象。另外,還新增了Proc#to_proc。
class Foo def to_proc p "should generate Proc object" end end def foo end foo(&Foo.new) => ruby 1.7.2 (2002-04-24) [i586-linux] "should generate Proc object" -:10: wrong argument type Foo (expected Proc) (TypeError)
從Fixnum, Integer移動(dòng)到這里。
新增。[ruby-dev:16909]
p /foo(bar)*/.to_s => "(?-mix:foo(bar)*)"
新增。返回文件名中的擴(kuò)展名。[ruby-talk:37617]
新增。
以前,若遇到比-2147483648還小的數(shù)值時(shí),其2進(jìn)制、8進(jìn)制、16進(jìn)制的表示形式就會(huì)出問(wèn)題 [ruby-list:34828]
p "%b" % -2147483648 p "%b" % -2147483649 p "%b" % -2147483650 => ruby 1.6.7 (2002-03-01) [i586-linux] "..10000000000000000000000000000000" "..1" "..10" => ruby 1.7.2 (2002-04-11) [i586-linux] "..10000000000000000000000000000000" "..101111111111111111111111111111111" "..101111111111111111111111111111110"
raise SystemExit時(shí),會(huì)使用結(jié)束狀態(tài)值 1 。 [ruby-dev:16776]
新增
新增 [ruby-talk:21612], [ruby-talk:36703]
在Net::HTTP 的類方法中,可以使用URI對(duì)象了。
Net::HTTP.get_print(URI.parse('http://www.ruby-lang.org/ja/'))
請(qǐng)注意,在實(shí)例方法中則無(wú)法使用。
在包含rescue/ensure的begin語(yǔ)句中,也可以使用while/until來(lái)進(jìn)行修飾了。
以前在包含rescue/ensure的while/until修飾表達(dá)式中,并沒(méi)有最先執(zhí)行主體部分(與C語(yǔ)言中的do ... while語(yǔ)法相同)。 [ruby-list:34618]
i = 0 begin p i i += 1 rescue end while i < 0 => ruby 1.6.7 (2002-03-01) [i586-linux] => ruby 1.7.2 (2002-03-29) [i586-linux] 0
不僅可以在方法定義中使用rescue/ensure,在類定義或模塊定義中也可以如此。
class Foo hogehoge rescue p $! end => -:3: parse error ruby 1.6.7 (2002-03-01) [i586-linux] => ruby 1.7.2 (2002-03-29) [i586-linux] #<NameError: undefined local variable or method `hogehoge' for Foo:Class>
已結(jié)束(aborting)的線程也被包含到列表中。 [rubyist:1282]
th = Thread.new {sleep} Thread.critical = true th.kill p Thread.list => ruby 1.6.7 (2002-03-01) [i586-linux] [#<Thread:0x401ba5c8 run>] => ruby 1.7.2 (2002-03-29) [i586-linux] [#<Thread:0x401b0618 aborting>, #<Thread:0x401ba0b4 run>]
若對(duì)已結(jié)束(aborting)的線程使用上述方法時(shí),該線程就會(huì)起死回生?,F(xiàn)在已經(jīng)修復(fù)了這個(gè)bug。 [rubyist:1282]
在sprintf 的 '%u' 中,不再新增".."了。[ruby-dev:16522]
p sprintf("%u", -1) => ruby 1.6.7 (2002-03-01) [i586-linux] "..4294967295" => -:1: warning: negative number for %u specifier ruby 1.7.2 (2002-03-29) [i586-linux] "4294967295"
打上了支持VMS的補(bǔ)丁。
新增了下列各庫(kù)。 iconv.so, tsort.rb, stringio.so, strscan.so, fileutils.rb, racc/*
可以指定Dir.glob的第2參數(shù)(決定匹配方式的標(biāo)識(shí))了。在Dir[] 中則無(wú)法使用該標(biāo)識(shí)。
新增了相關(guān)的常數(shù) File::FNM_DOTMATCH (表示 與FNM_PERIOD相反)。
p Dir.glob("/*") => ruby 1.7.2 (2002-03-15) [i586-linux] ["/lost+found", "/root", ...] p Dir.glob("/*", File::FNM_DOTMATCH) => ruby 1.7.2 (2002-03-15) [i586-linux] ["/.", "/..", "/lost+found", "/root", "/boot", ...]
可以正確處理large file(大小超過(guò)4G bytes的文件)了(真的?) [ruby-talk:35316], [ruby-talk:35470]
在mswin32, mingw32中,也可以使用Process.kill(9, pid)來(lái)強(qiáng)制結(jié)束進(jìn)程(TerminateProcess)。(好像Process.kill("KILL", pid)就不行???2002-08-28 以后好像可以使用 "KILL" 了)
新增
可以指定結(jié)束消息了。
abort("abort!") => abort! ruby 1.7.2 (2002-03-15) [i586-linux]
您所指定的消息會(huì)被設(shè)置給異常SystemExit對(duì)象的message 屬性。
begin abort("abort!") rescue SystemExit p $!.message end => abort! ruby 1.7.2 (2002-03-29) [i586-linux] "abort!"
文檔中沒(méi)有提到 [ruby-dev:16126]
傳遞若干個(gè)模塊時(shí),include的順序有所改變。好像[ruby-dev:16035] extend 也是如此。[ruby-dev:16183]
module Foo; end module Bar; end module Baz; end include Foo, Bar, Baz p Object.ancestors => ruby 1.6.7 (2002-03-01) [i586-linux] [Object, Baz, Bar, Foo, Kernel] => ruby 1.7.2 (2002-03-01) [i586-linux] [Object, Foo, Bar, Baz, Kernel] obj = Object.new module Foo; end module Bar; end module Baz; end obj.extend Foo, Bar, Baz class << obj p ancestors end => ruby 1.6.7 (2002-03-01) [i586-linux] [Baz, Bar, Foo, Object, Kernel] => ruby 1.7.2 (2002-03-08) [i586-linux] [Foo, Bar, Baz, Object, Kernel]
這與一個(gè)一個(gè)地include時(shí)的順序相反。
module Foo; end module Bar; end module Baz; end include Foo include Bar include Baz p Object.ancestors => ruby 1.7.2 (2002-03-01) [i586-linux] [Object, Baz, Bar, Foo, Kernel]
新增
[ruby-bugs-ja:PR#98] (2003-03-11: 該修改被取消 [ruby-dev:19799]) (其后Proc#yield也被取消)
Proc.new { break }.call Proc.new { break }.yield => -:2:in `yield': break from proc-closure (LocalJumpError) from -:2 ruby 1.7.3 (2002-09-05) [i586-linux] => ruby 1.8.0 (2003-03-12) [i586-linux]
可以在pack/unpack 的模板中寫(xiě)入注釋了。
p [1,2,3,4].pack("s # short (fixed 2 bytes) i # int (machine dependent) l # long (fixed 4 bytes) q # quad (fixed 8 bytes)") => ruby 1.7.2 (2002-02-21) [i586-linux] "\001\000\002\000\000\000\003\000\000\000\004\000\000\000\000\000\000\000"
新增(其后變?yōu)閑xit_value)
def foo proc { return 10 } end begin foo.call rescue LocalJumpError p $!.exitstatus end => ruby 1.7.2 (2002-02-14) [i586-linux] 10
新增。與Socket#listen相同。
新增
比較兩個(gè)沒(méi)有繼承關(guān)系的類/模塊時(shí),將返回nil。
p Array <=> String => ruby 1.6.7 (2002-03-01) [i586-linux] 1 => ruby 1.7.3 (2002-09-13) [i586-linux] nil
新增
新征了64 bit 整數(shù)的模板字符 Q/q (表示Quad之意)。 Q表示unsigned,而q表示signed。 與perl不同的是,即使在不支持64 bit 整數(shù)的平臺(tái)上,仍然可以使用。
p [ 1].pack("Q") p [-1].pack("Q") p [ 1].pack("q") p [-1].pack("q") p [ 1].pack("Q").unpack("Q") p [-1].pack("Q").unpack("Q") p [ 1].pack("q").unpack("q") p [-1].pack("q").unpack("q") => ruby 1.7.2 (2002-02-13) [i586-linux] "\001\000\000\000\000\000\000\000" "\377\377\377\377\377\377\377\377" "\001\000\000\000\000\000\000\000" "\377\377\377\377\377\377\377\377" [1] [18446744073709551615] [1] [-1]
特殊方法的輸出形式更具實(shí)際意義了。 [ruby-bugs-ja:PR#193]
obj = [] def obj.foo end p obj.method(:foo) => ruby 1.6.6 (2001-12-26) [i586-linux] #<Method: Array(Array)#foo> => ruby 1.7.2 (2002-02-05) [i586-linux] #<Method: [].foo>
可以將塊的計(jì)算結(jié)果指定為fill值。依次為各個(gè)元素計(jì)算塊的內(nèi)容,所以下列中每次都會(huì)生成"val"
ary = Array.new(3, "val") p ary.collect {|v| v.id } # => [537774036, 537774036, 537774036] ary = Array.new(3) { "val" } p ary.collect {|v| v.id } # => [537770040, 537770028, 537770016]
新增
s = File.stat("/dev/null") p s.rdev_major p s.rdev_minor => ruby 1.7.2 (2002-01-28) [i686-linux] 1 3
可以指定塊了。還可以控制重復(fù)鍵的處理方式。
新增
當(dāng)$SAFE為1或2時(shí),被污染的Proc將無(wú)法變成塊 [ruby-dev:15682]
$SAFE = 1 proc = proc {} proc.taint p proc.tainted? def foo(&b) p b.tainted? end foo(&proc) => ruby 1.6.8 (2003-08-03) [i586-linux] true true => ruby 1.7.2 (2002-01-23) [i586-linux] true true
可以將基數(shù)(2,8,10,16)指定給參數(shù)。(2002-01-26: 當(dāng)參數(shù)為0時(shí),用prefix來(lái)判定基數(shù))
p "010".to_i(16) => ruby 1.7.2 (2002-01-11) [i586-linux] 16
可以把塊當(dāng)作哈希的默認(rèn)值了。指定塊之后,每次使用空的哈希元素時(shí)都會(huì)執(zhí)行塊的內(nèi)容,并返回其結(jié)果。此時(shí)會(huì)把 哈希本身 和 使用哈希時(shí)的鍵 傳給塊。
h = Hash.new("foo") p h.default.id p h.default(0).id # Hash#default 可以指定傳給塊的鍵 p h[0].id p h[0].id p h[1].id => ruby 1.7.2 (2001-12-10) [i586-linux] 537774276 537774276 537774276 537774276 h = Hash.new { "foo" } p h.default.id p h.default(0).id p h[0].id p h[0].id p h[1].id => ruby 1.7.2 (2001-12-10) [i586-linux] 537770616 537770352 537770316 537770280 h = Hash.new { raise IndexError, "undefined!!" } p h[0] => -:1: undefined!! (IndexError) from -:1:in `yield' from -:2:in `default' from -:2:in `[]' from -:2 ruby 1.7.2 (2001-12-10) [i586-linux]
新增(此后被values_at方法所取代)
# 沒(méi)有給出塊的話,則與indexes/indicies 相同。 # (注: indexes/indicies已變?yōu)閛bsolete) p [1,2,3].select(0,1,2,3) p [1,2,3].select(-4,-3,-2,-1) p( {1=>"a", 2=>"b", 3=>"c"}.select(3,2,1) ) => ruby 1.7.2 (2001-12-10) [i586-linux] [1, 2, 3, nil] [nil, 1, 2, 3] ["c", "b", "a"] # 若給出了塊的話,則與Enumerable#select 相同。 p [1,2,3,4,5].select {|v| v % 2 == 1} p( {1=>"a", 2=>"b", 3=>"c"}.select {|k,v| k % 2 == 1} ) => ruby 1.6.6 (2001-12-04) [i586-linux] [1, 3, 5] [[1, "a"], [3, "c"]] => ruby 1.7.2 (2001-12-10) [i586-linux] [1, 3, 5] [[1, "a"], [3, "c"]] m = /(foo)(bar)(baz)/.match("foobarbaz") p m.select(0,1,2,3,4) # same as m.to_a.indexes(...) p m.select(-1,-2,-3) => ruby 1.7.2 (2001-12-10) [i586-linux] ["foobarbaz", "foo", "bar", "baz", nil] ["baz", "bar", "foo"]
新增。與 re.match(str) 相同。
對(duì)Float進(jìn)行dump時(shí),不再依賴sprintf(3)了。format version由4.6升到4.7。 (此后,由于把strtod(3)收入了語(yǔ)言本身,即使進(jìn)行讀入時(shí)也不再依賴strtod(3)了)
如將受污染的字符串傳給第二參數(shù)的話,就會(huì)引發(fā)SecurityError異常。在1.6中,若安全級(jí)別為4則會(huì)計(jì)算受污染的字符串。 [ruby-list:32215]
在Module.new, Class.new中,若給出了塊的話,則在生成的模塊/類的上下文中計(jì)算塊的內(nèi)容。
Module.new {|m| p m} => ruby 1.7.1 (2001-10-15) [i586-linux] #<Module:0x401afd5c>
不能對(duì)Numeric這種immutable對(duì)象進(jìn)行clone。 [ruby-bugs-ja:PR#94], [rubyist:0831]
$DEBUG=true true.clone rescue nil false.clone rescue nil nil.clone rescue nil :sym.clone rescue nil (10**10).clone rescue nil 0.clone rescue nil => Exception `TypeError' at -:2 - can't clone true Exception `TypeError' at -:3 - can't clone false Exception `TypeError' at -:4 - can't clone nil Exception `TypeError' at -:5 - can't clone Symbol ruby 1.6.6 (2001-12-26) [i586-linux] => Exception `TypeError' at -:2 - can't clone TrueClass Exception `TypeError' at -:3 - can't clone FalseClass Exception `TypeError' at -:4 - can't clone NilClass Exception `TypeError' at -:5 - can't clone Symbol Exception `TypeError' at -:6 - can't clone Bignum Exception `TypeError' at -:7 - can't clone Fixnum ruby 1.7.1 (2001-10-10) [i586-linux]
puts不再對(duì)數(shù)組進(jìn)行特殊處理了,而是輸出Array#to_s。Array#to_s在默認(rèn)情況下會(huì)輸出帶換行的字符串,所以從其輸出形式看來(lái)是沒(méi)有什么變化的(但要受到$,的值的影響)。[ruby-dev:15043]
該修改尚處于試驗(yàn)階段,又可能會(huì)改回原樣。。。[ruby-dev:15313]
$, = "," puts %w(foo bar baz) => ruby 1.6.5 (2001-11-01) [i586-linux] foo bar baz => ruby 1.7.2 (2001-11-25) [i586-linux] foo,bar,baz
???好像是改回原樣了。
=> ruby 1.7.2 (2001-12-10) [i586-linux] foo bar baz
可以將基數(shù)指定給參數(shù)了。
p 10.to_s(16) => ruby 1.7.2 (2001-11-25) [i586-linux] "a"
只要$/ 的值為"\n" (默認(rèn)),不管是哪種行尾("\r\n", "\r"或"\n")都可以清除干凈。
p "aaa\r\n".chomp => ruby 1.6.5 (2001-11-01) [i586-linux] "aaa\r" => ruby 1.7.2 (2001-11-25) [i586-linux] "aaa"
Complex#to_i, #to_f, #to_r已被取消。 [ruby-bugs-ja:PR#102], [rubyist:0879]
若方法名和括弧之間有空格,則該括號(hào)不會(huì)為當(dāng)作是包含參數(shù)的括號(hào),而會(huì)被當(dāng)作表達(dá)式中的括號(hào)。
p (1+2)*3 => -:1: warning: p (...) interpreted as method call -:1: warning: useless use of * in void context ruby 1.6.5 (2001-09-19) [i586-linux] 3 -:1: undefined method `*' for nil (NameError) => -:1: warning: p (...) interpreted as command call ruby 1.7.1 (2001-06-05) [i586-linux] 9
若對(duì)結(jié)構(gòu)體類的子類進(jìn)行dump的話,則無(wú)法讀出。[ruby-bugs-ja:PR#104]
S = Struct.new("S", :a) class C < S end p Marshal.load(Marshal.dump(C.new)) => -:4: warning: instance variable __member__ not initialized -:4:in `dump': uninitialized struct (TypeError) from -:4 ruby 1.6.5 (2001-09-19) [i586-linux] => ruby 1.7.1 (2001-10-19) [i586-linux] #<C a=nil>
全局變量的別名無(wú)效。 [ruby-dev:14922]
$g2 = 1 alias $g1 $g2 p [$g1, $g2] $g2 = 2 p [$g1, $g2] => ruby 1.6.5 (2001-09-19) [i586-linux] [1, 1] [1, 2] => ruby 1.7.1 (2001-10-19) [i586-linux] [1, 1] [2, 2]
新增
String#[re, idx] String#[re, idx] = val
新增第二個(gè)可選參數(shù)idx。
p "foobarbaz"[/(foo)(bar)(baz)/, 1] p /(foo)(bar)(baz)/.match("foobarbaz").to_a[1] => -:2: warning: ambiguous first argument; make sure ruby 1.7.1 (2001-10-05) [i586-linux] "foo" "foo" str = "foobarbaz" p str[/(foo)(bar)(baz)/, 2] = "BAR" # => "BAR" p str # => "fooBARbaz"
str[/re/, 0] 與 str[/re/] 相同。
由allocate 和 initialize 這兩個(gè)方法來(lái)生成對(duì)象。[ruby-dev:14847] 請(qǐng)參考 rb_define_alloc_func() 。
若將數(shù)組傳給Array.new 的參數(shù)時(shí),將會(huì)生成該數(shù)組的拷貝。
ary = [1,2,3] ary2 = Array.new ary p ary, ary2 p ary.id, ary2.id => ruby 1.7.1 (2001-10-05) [i586-linux] [1, 2, 3] [1, 2, 3] 537758120 537755360
可以省略String.new 的參數(shù)了。
p String.new => -:1:in `initialize': wrong # of arguments(0 for 1) (ArgumentError) from -:1:in `new' from -:1 ruby 1.7.1 (2001-08-29) [i586-linux] => ruby 1.7.1 (2001-10-05) [i586-linux] ""
新增
p Dir.open(".").path => ruby 1.7.1 (2001-10-05) [i586-linux] "."
在Readline.readline的執(zhí)行過(guò)程中使用Ctrl-C進(jìn)行中斷之后,還可以恢復(fù)終端狀態(tài)。[ruby-dev:14574]
新增(Module#included的重定義)
新增。
while, until, class, def能返回值了。
class/module 返回最后計(jì)算的表達(dá)式的值。def返回nil。while/until通常返回 nil,若使用帶參數(shù)的break的話,則可以返回任何值。
p(while false; p nil end) p(while true; break "bar" end) p(class Foo; true end) p(module Bar; true end) p(def foo; true end) => -:1: void value expression -:2: void value expression -:3: void value expression -:4: void value expression -:5: void value expression ruby 1.7.1 (2001-08-20) [i586-linux] => -:1: warning: void value expression -:2: warning: void value expression -:3: warning: void value expression -:4: warning: void value expression -:5: warning: void value expression ruby 1.7.1 (2001-08-23) [i586-linux] false "bar" true true nil
修正后,while/until會(huì)在途中返回nil。 [ruby-dev:15909]
=> -:1: warning: void value expression -:2: warning: void value expression -:3: warning: void value expression -:4: warning: void value expression -:5: warning: void value expression ruby 1.7.2 (2002-02-20) [i586-linux] nil "bar" true true nil
以前在對(duì) 字符串的范圍對(duì)象 和 字符串 進(jìn)行比較時(shí),只會(huì)與范圍的兩段作比較。而現(xiàn)在則使用String#upto來(lái)和每個(gè)元素進(jìn)行比較。
(2002-06-04: 之后作的修改)
p(("a" .. "ab") === "aa") => ruby 1.7.1 (2001-08-20) [i586-linux] true => ruby 1.7.1 (2001-08-23) [i586-linux] false
新增。這是為處理[ruby-dev:8986]之后提到的Schwartzian transform而準(zhǔn)備的sort。
Updated. New methods and constants for using the mouse, character attributes, colors and key codes have been added.
新增。每隔step就使用對(duì)應(yīng)元素進(jìn)行迭代。
條件式中的正則表達(dá)式字面值 會(huì)給出警告。
在于$_ 進(jìn)行正則表達(dá)式匹配時(shí),推薦您這樣顯式地處理 ~/re/ (單項(xiàng)的 ~方法)。
$_ = "foo" p $_ if /foo/ p $_ if /bar/ => -:2: warning: regex literal in condition -:3: warning: regex literal in condition ruby 1.7.1 (2001-08-14) [i586-linux] "foo"
新增。去除左端或右端的空格。
新增。套接字地址結(jié)構(gòu)體(INET domain)的pack/unpack。
新增。套接字地址結(jié)構(gòu)體(UNIX domain)的pack/unpack。
新增。忽略字母的大小寫(xiě)區(qū)別來(lái)比較字符串。
不管$=的值如何,總是區(qū)分字母的大小寫(xiě)。
新增 [ruby-dev:13941]
Changed to warn only when invoked from multiple threads or no block is given. [ruby-dev:13823]
Dir.chdir("/tmp") pwd = Dir.pwd #=> "/tmp" puts pwd Dir.chdir("foo") { pwd = Dir.pwd #=> "/tmp/foo" puts pwd Dir.chdir("bar") { # <-- previously warned pwd = Dir.pwd #=> "/tmp/foo/bar" puts pwd } pwd = Dir.pwd #=> "/tmp/foo" puts pwd } pwd = Dir.pwd #=> "/tmp" puts pwd
新增 [ruby-dev:13597] (之后又被刪除了)
除去它不檢查參數(shù)的個(gè)數(shù)之外,其他則與Proc#call 相同。
新增
在File::Constants模塊中定義了該方法所用的FNM_NOESCAPE, FNM_PATHNAME, FNM_PERIOD, FNM_CASEFOLD標(biāo)識(shí)。
p %w(foo bar bar.bak).reject! { |fn| File::fnmatch?("*.bak", fn) } => ruby 1.7.1 (2001-06-12) [i586-linux] ["foo", "bar"]
新增
修改了多重賦值的規(guī)則。變更如下。
# *a = nil p a => ruby 1.7.1 (2001-06-05) [i586-linux] [nil] => ruby 1.7.1 (2001-06-12) [i586-linux] [] => ruby 1.8.0 (2003-01-18) [i586-linux] [nil]
但好像是又改回去了。與2003-01-07 的 eval.c的塊參數(shù)比較起來(lái),它應(yīng)該是正確的。
def foo yield nil yield end foo {|*a| p a} => ruby 1.6.8 (2002-12-24) [i586-linux] [nil] [nil] => ruby 1.8.0 (2003-08-04) [i586-linux] [nil] []
修正了下列問(wèn)題?,F(xiàn)在,只包含1元素的數(shù)組也能被正常展開(kāi)。
a = *[1] p a #=> [1] => ruby 1.6.8 (2002-12-24) [i586-linux] [1] => ruby 1.7.1 (2001-06-05) [i586-linux] 1
重新回到NameError を StandardError 的子類中去。類的層次關(guān)系如下。
NoMethodError < NameError < StandardError.
以前,只有將第2參數(shù)指定為數(shù)值(File::RDONLY|File::CREAT等)時(shí),才會(huì)使用第3參數(shù)。現(xiàn)在只要給出了第3參數(shù),就總是有效的。[ruby-bugs-ja:PR#54]
由于使用了內(nèi)部的哈希表,所以使用常數(shù)的速度有所提升。(對(duì)應(yīng)于ChangeLog的
Tue Jun 5 16:15:58 2001 Yukihiro Matsumoto <matz@ruby-lang.org>
部分)
下列代碼(請(qǐng)注意p后面的空格)
p ("xx"*2).to_i
不會(huì)被解生成
(p("xx"*2)).to_i
而是
p (("xx"*2).to_i)
這樣(這只是試驗(yàn)性的修改)。
新增。現(xiàn)在(因?yàn)闀?huì)自動(dòng)進(jìn)行數(shù)組變換,所以)可以寫(xiě)成這樣。(2003-03-29: 此后Range#to_ary就被刪除了)
a, b, c = 1..3 p [a, b, c] => ruby 1.6.8 (2002-12-24) [i586-linux] [1..3, nil, nil] => ruby 1.7.3 (2002-12-11) [i586-linux] [1, 2, 3] => ruby 1.8.0 (2003-03-29) [i586-linux] [1..3, nil, nil]
使用參數(shù)之后,break, next就可以返回 迭代器 或 yield 的值了。(該功能尚處于試驗(yàn)階段)
break [n]會(huì)終止迭代器,n就是迭代器的返回值。 而next [n]會(huì)跳到塊的外面,n就是yield的返回值。
def foo p yield end foo { next 1 } def bar yield end p bar { break "foo" } => ruby 1.7.1 (2001-08-20) [i586-linux] 1 "foo"
定義了to_str的對(duì)象將在更大的空間內(nèi)扮演String的角色。
大部分將字符串作為參數(shù)的內(nèi)部方法都在嘗試調(diào)用to_str進(jìn)行隱式的類型變換。
foo = Object.new class <<foo def to_str "foo" end end p File.open(foo) => -:7:in `open': wrong argument type Object (expected String) (TypeError) ruby 1.6.4 (2001-04-19) [i586-linux] => -:7:in `open': No such file or directory - "foo" (Errno::ENOENT) ruby 1.7.0 (2001-05-02) [i586-linux]
如果把一個(gè)包含to_str方法的非字符串對(duì)象傳給擴(kuò)展庫(kù)的API--STR2CSTR()的話,它就會(huì)在內(nèi)部調(diào)用to_str,進(jìn)行隱式的類型轉(zhuǎn)換。此時(shí)雖然能返回變換結(jié)果所保持的字符串指針,但在該API中,這個(gè)隱式變換的結(jié)果不會(huì)被任何對(duì)象所使用,因此可能會(huì)被GC回收掉。 [ruby-dev:12731]
version 1.7以后,使用StringValuePtr()來(lái)替代它。此時(shí),參數(shù)所用的對(duì)象會(huì)被隱式轉(zhuǎn)換結(jié)果所替代,因此變換結(jié)果不會(huì)被GC所回收。(在version 1.7中,STR2CSTR()變成obsolete)
另外還準(zhǔn)備了一個(gè)新的API--StringValue()。在需要對(duì)參數(shù)進(jìn)行to_str的隱式類型變換時(shí)使用它。若參數(shù)是字符串的話,則不進(jìn)行任何操作。把它用在處理字符串的方法的前面則會(huì)相當(dāng)方便。
目前還沒(méi)有開(kāi)發(fā)出替代str2cstr() (返回C指針和字符串長(zhǎng)度)的安全的API。(在[ruby-dev:15644]中有一些建議)
只有在使用-e選項(xiàng)的單行腳本中,才能對(duì) 范圍操作符表達(dá)式中的單個(gè)數(shù)值字面值 和 $.
進(jìn)行比較。
使用Module#===來(lái)比較 發(fā)生的異常$! 和 rescue中的異常類。
以前使用kind_of?來(lái)進(jìn)行比較時(shí),基本上也沒(méi)有什么問(wèn)題。這里主要是對(duì)SystemCallError.===進(jìn)行了重定義,只要兩個(gè)異常的errno相同就把它們看作是同一個(gè)異常。因此,若Errno::EWOULDBLOCK 和 Errno::EAGAIN的意義相同(相同的errno)時(shí),不管指定哪個(gè),都可以進(jìn)行rescue。
后來(lái),只要兩個(gè)Errno::XXX對(duì)象的errno相同,就被看作是相同的異常,因此這個(gè)修改也就失效了,但卻一直保留下來(lái)。 (或許在用戶定義異常類時(shí)還能用到) [ruby-dev:19589]
在不帶塊的時(shí)候,Array#collect會(huì)返回self.dup。因此可能會(huì)返回Array以外的對(duì)象[ruby-list:30480]。
Foo = Class.new Array a = Foo.new p a.map.class p a.collect.class => ruby 1.7.1 (2001-06-12) [i586-linux] Array Foo => ruby 1.7.1 (2001-07-31) [i586-linux] Array Array
修正了dup中的bug [ruby-list:30481] (1.6 中也進(jìn)行了修正)
class Foo < Array attr_accessor :foo end a = Foo.new a.foo = 1 b = a.dup b.foo b.foo = 99 p b.foo # => ruby 1.6.4 (2001-06-04) [i586-linux] nil # => ruby 1.6.4 (2001-07-31) [i586-linux] 99
新增
新增 [ruby-talk:14289]
與ary[n,0] = [other,...]
相同(但卻返回self)
ary = [0,1,2,3] ary[2, 0] = [4, 5, 6] p ary ary = [0,1,2,3] ary.insert(2, 4, 5, 6) p ary
Array#pack, String#unpack 的模板字符"p", "P"可以對(duì)nil和NULL指針進(jìn)行互換了。[ruby-dev:13017]。
p [nil].pack("p") p "\0\0\0\0".unpack("p") => ruby 1.7.0 (2001-05-17) [i586-linux] "\000\000\000\000" [nil]
總是返回self。
不能保證將來(lái)一直如此 [ruby-dev:12506]。
(注: 并非Class#inherited)
以前為了禁止類定義子類而設(shè)立該方法,(其作用是引發(fā)TypeError異常)現(xiàn)在由Class.new來(lái)完成這個(gè)任務(wù),所以Class.inherited方法就被刪除了。
class SubClass < Class end #=> -:1:in `inherited': can't make subclass of Class (TypeError) from -:1 ruby 1.7.1 (2001-06-12) [i586-linux] #=> -:1: can't make subclass of Class (TypeError) ruby 1.7.1 (2001-07-31) [i586-linux]
若帶塊的話,則與File.open相同,塊的結(jié)果成為方法的返回值。(在1.6以前,其返回值恒為nil
)
可以指定塊了。
可以使用前面的反斜線來(lái)對(duì)通配符進(jìn)行轉(zhuǎn)義處理。另外,空格也不再具有特殊意義('\0'依然有效)。
新增
新增
新增
新增。主要是實(shí)現(xiàn)了[ruby-talk:9460]中提到的功能
Interrupt 成為SignalException的子類。(在1.6以前,它是Exception的子類)
新增。Marshal輸出的dump format的版本號(hào)。 [ruby-dev:14172]
新增 [ruby-dev:12766] (2003-03-28: 后來(lái)又消失了)
為了方便Regexp#match而設(shè)。以前必須得
foo, bar, baz = /(\w+?)\s+(\w+?)\s+(\w+)/.match("foo bar baz").to_a[1..-1] p [foo, bar, baz]
這樣才行?,F(xiàn)在可以
_, foo, bar, baz = /(\w+?)\s+(\w+?)\s+(\w+)/.match("foo bar baz") p [foo, bar, baz]
這樣。
新增
新增。在Module#append_feature后調(diào)用的hook
新增
新增。返回商。
(之后變成div(?))
新增 [ruby-dev:12763]
舊稱被刪除。請(qǐng)使用NotImplementedError
新增
新增了可選參數(shù)all。
class Foo def foo end end obj = Foo.new module Bar def bar end end class <<obj include Bar def baz end end p obj.singleton_methods #=> ["baz"] p obj.singleton_methods true #=> ["baz", "bar"]
從Time.times移到這里。(Time.times還在,但會(huì)出現(xiàn)warning)
新增。$?
的值從整數(shù)中分離出來(lái),變成該類的實(shí)例。
新增
新增
新增了可選參數(shù)。
新增
新增。比較字符串而不區(qū)分大小寫(xiě)。
新增
與str[n, 0] = other
相同(但會(huì)返回 self)
新增 [ruby-dev:12921]
新增(???之后又被刪除)
新增 (請(qǐng)參考上面的「rescue 節(jié)的...」內(nèi)容) [ruby-dev:12670]
新增
可使用第3,4參數(shù)來(lái)進(jìn)行指定。
新增。返回Thread固有數(shù)據(jù)的鍵的數(shù)組。
可以處理負(fù)的 time_t (僅限于OS支持的場(chǎng)合)
p Time.at(-1) => Thu Jan 01 08:59:59 JST 1970
對(duì)gmtime 時(shí)區(qū)返回UTC" (以前取決于系統(tǒng)環(huán)境,大部分場(chǎng)合返回"GMT")