?
このドキュメントでは、 php中國語ネットマニュアル リリース
ruby version 1.7是開發(fā)版。將來可能會刪除下列中的部分內(nèi)容,也可能因為兼容性問題而對其進行修改。
新增。作為profile.rb的實體將其分離出來。
新增。用在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
遇到對屬性賦值或對數(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的支持補丁。
向IO#read, IO#sysread新增了第二參數(shù)(指定了預先分配好的讀入緩沖)
新增。與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]]
試驗性的修改。
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的關系 取得一致)
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]
采用了符號的擴展表示法。[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)先級。好像是試驗性的修改。 (在1.8版本中正式采用了這個修改)。因此
a = b rescue c
不會被解釋成
(a = b) rescue c
而是被解釋為
a = (b rescue c)
雖然與if修飾部分的優(yōu)先級有所不同,但它有個好處:如果b發(fā)生異常時可以使用c的值。
# 若在以前的版本(1.6)中執(zhí)行下列代碼時,則不會進行賦值 # 只是對變量進行了定義,結果是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
使用它就會出現(xiàn)警告。請您使用Object#class來代替它。
p Object.new.type => -:1: warning: Object#type is deprecated; use Object#class ruby 1.7.3 (2002-10-08) [i586-linux] Object
在類定義表達式的末尾才會調(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!"
若在方法定義的外側調(diào)用return的話,則會在運行時而非編譯時引發(fā)錯誤。
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
以前,使用||=對未定義的變量進行賦值時,會在全局變量中出現(xiàn)警告。另外,在類變量中會引發(fā)錯誤。 [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會在內(nèi)部將進程ID變?yōu)檎龜?shù)。雖然在NT系列的OS中沒有什么變化,但在Win9x系列的OS中,由OS控制的進程ID是負數(shù),所以才將其變?yōu)檎龜?shù)。[ruby-dev:18263]
在對File::NONBLOCK模式的IO進行讀入操作時,如果發(fā)生EWOULDBLOCK的話,可能會導致讀入數(shù)據(jù)丟失。 [ruby-dev:17855]
在使用Thread的程序中,如果從文件中讀出數(shù)據(jù)并寫入socket時,可能會在Socket#write中引發(fā)Errno::EINTR,但這種情況極少出現(xiàn)。[ruby-dev:17878], [ruby-core:00444]
無法對包含(include)了無名模塊的對象進行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)了有名模塊的對象,此時模塊的信息被保存到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)
開始著手合并extmk.rb和mkmf.rb。extmk.rb將會用到mkmf.rb。相應地對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>]
另外好像說,對象的特殊類的超類的特殊類 和 對象的特殊類的特殊類的超類是一回事兒[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]中好像還有些變化 (請參考2002-09-21的ChangeLog。)
新增
新增(后來改名為copy_object。再后來又改名為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中的擴展庫的兼容性。分別把Config::CONFIG['RUBY_SO_NAME']變更為msvcrt-rubyXX(成為DLL名),把Config::CONFIG['sitearch'](擴展庫所在地的路徑元素)變更為"i386-msvcrt"。 [ruby-dev:17144], [ruby-dev:18047]
在這次修改中,新增了sitearch(在其他環(huán)境中,則與CONFIG['arch']相同)
另外請參考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 的結果中新增了腳本的源文件名和行號。[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]
另外,若沒有找到元素的話,就返回ifnone 的結果。
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
新增。
在生成隨機數(shù)的算法中使用了Mersenne Twister。
允許對方法定義進行嵌套。
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
在方法定義外側調(diào)用super時,將會在運行時而不是編譯時引發(fā)錯誤。
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進制整數(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]
允許使用大寫字母。
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進制字面值的0和0o前綴。
p 0o377 => ruby 1.7.3 (2002-09-04) [i586-linux] 255
可以使用大寫字母。
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()也沒問題。
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
若將字符串而非正則表達式傳給pattern的話,就直接把它用作匹配模型,而不會將其編譯成正則表達式。(準確地講,不會Regexp.compile(arg)這樣處理,而是Regexp.compile(Regexp.quote(arg))這樣)
只有str =~ arg中的arg是字符串時,才會執(zhí)行str.index(arg),它等價于Regexp.compile(Regexp.quote(arg)) =~ str(所以沒有設定$~)。
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 = 方法
聽說加入了支持Win32用的雙向管道的補丁 [ruby-win32:185]
使用它的話會出現(xiàn)警告消息。(聽說已經(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)中,又開始接受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()不同的是,它可以使用反斜線表示法和展開式。[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ù)值或字符串以外的對象變換為整數(shù)時,不再使用to_i,而是使用to_int進行變換。
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]
在#{ ... }展開式中,可以書寫任何ruby程序,包括字符串分隔符在內(nèi)。雖然以前也是如此,但此次則明確了規(guī)則。也就是說,展開式中的語法規(guī)則與外面相同。ruby程序會被正確解析。[ruby-dev:17422]
(1.6 版本中,曾出現(xiàn)過異常的舉動)
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"
不應該對下列分隔符進行轉義。
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"
請注意:展開式中注釋并不是從 # 到 } ,而是從 # 到換行。
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__ 不再被當作腳本的結束標志了。[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 等不再是字面值。若必須使用的話,可以寫成 ?\s, ?\n, ?\t 。(請注意,下例中的前半部分使用了雙引號) [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解釋器的補丁。
Range#max, Range#min, Range#include? 使用 <=> 方法進行范圍計算。[ruby-list:35253], [ruby-dev:17228] (2003-03-18: min, max 變回原樣。[ruby-dev:19837])
Range#member? 使用 each 來遍歷所有元素并確認是否有member。(與Enumerable#member?相同)
截止1.6,max, min, member? include? 是 Enumerable 的方法,=== 是 Range的方法。在1.7中,max, min, member?, include?, === 都是 Range 的方法,include? 成了 === 的別名。(在1.8中,max, min重新成為 Enumerable 的方法)
因為這些變動導致下列不同。
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 方法進行迭代操作。
(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的元素數(shù)量,必須這樣
p(("a".."z").to_a.size) => ruby 1.7.2 (2002-08-01) [i586-linux] 26
才行。
新增
2003-01-21: 該修改好像是變回原來的樣子
對 -數(shù)值 的字面值的解釋發(fā)生了變化,-數(shù)值 總是被當作一個字面值來處理。
例如,下面的表達式的結果就各不相同。
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)先級不同所致 (真的如此嗎?)。在1.7中則被解釋成(-2)**2。另外,若在 - 和數(shù)值之間插入空格的話, - 會被當作單項操作符(方法)。(這與以前相同)
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數(shù)時,不再依賴庫函數(shù) strtod(3)了。這樣,即使修改了庫的內(nèi)容,也不會影響到它的運作。
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來指定等待線程的時間。
新增
在使用 & 來修飾方法的參數(shù)時,若傳給參數(shù)的對象中包含to_proc 方法就調(diào)用它,并把結果當作塊來傳給方法。以前,& 就只能修飾Proc, Method對象。另外,還新增了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移動到這里。
新增。[ruby-dev:16909]
p /foo(bar)*/.to_s => "(?-mix:foo(bar)*)"
新增。返回文件名中的擴展名。[ruby-talk:37617]
新增。
以前,若遇到比-2147483648還小的數(shù)值時,其2進制、8進制、16進制的表示形式就會出問題 [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時,會使用結束狀態(tài)值 1 。 [ruby-dev:16776]
新增
新增 [ruby-talk:21612], [ruby-talk:36703]
在Net::HTTP 的類方法中,可以使用URI對象了。
Net::HTTP.get_print(URI.parse('http://www.ruby-lang.org/ja/'))
請注意,在實例方法中則無法使用。
在包含rescue/ensure的begin語句中,也可以使用while/until來進行修飾了。
以前在包含rescue/ensure的while/until修飾表達式中,并沒有最先執(zhí)行主體部分(與C語言中的do ... while語法相同)。 [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>
已結束(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>]
若對已結束(aborting)的線程使用上述方法時,該線程就會起死回生?,F(xiàn)在已經(jīng)修復了這個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的補丁。
新增了下列各庫。 iconv.so, tsort.rb, stringio.so, strscan.so, fileutils.rb, racc/*
可以指定Dir.glob的第2參數(shù)(決定匹配方式的標識)了。在Dir[] 中則無法使用該標識。
新增了相關的常數(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(大小超過4G bytes的文件)了(真的?) [ruby-talk:35316], [ruby-talk:35470]
在mswin32, mingw32中,也可以使用Process.kill(9, pid)來強制結束進程(TerminateProcess)。(好像Process.kill("KILL", pid)就不行???2002-08-28 以后好像可以使用 "KILL" 了)
新增
可以指定結束消息了。
abort("abort!") => abort! ruby 1.7.2 (2002-03-15) [i586-linux]
您所指定的消息會被設置給異常SystemExit對象的message 屬性。
begin abort("abort!") rescue SystemExit p $!.message end => abort! ruby 1.7.2 (2002-03-29) [i586-linux] "abort!"
文檔中沒有提到 [ruby-dev:16126]
傳遞若干個模塊時,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]
這與一個一個地include時的順序相反。
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 的模板中寫入注釋了。
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相同。
新增
比較兩個沒有繼承關系的類/模塊時,將返回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ù)的平臺上,仍然可以使用。
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]
特殊方法的輸出形式更具實際意義了。 [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>
可以將塊的計算結果指定為fill值。依次為各個元素計算塊的內(nèi)容,所以下列中每次都會生成"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
可以指定塊了。還可以控制重復鍵的處理方式。
新增
當$SAFE為1或2時,被污染的Proc將無法變成塊 [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: 當參數(shù)為0時,用prefix來判定基數(shù))
p "010".to_i(16) => ruby 1.7.2 (2002-01-11) [i586-linux] 16
可以把塊當作哈希的默認值了。指定塊之后,每次使用空的哈希元素時都會執(zhí)行塊的內(nèi)容,并返回其結果。此時會把 哈希本身 和 使用哈希時的鍵 傳給塊。
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方法所取代)
# 沒有給出塊的話,則與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) 相同。
對Float進行dump時,不再依賴sprintf(3)了。format version由4.6升到4.7。 (此后,由于把strtod(3)收入了語言本身,即使進行讀入時也不再依賴strtod(3)了)
如將受污染的字符串傳給第二參數(shù)的話,就會引發(fā)SecurityError異常。在1.6中,若安全級別為4則會計算受污染的字符串。 [ruby-list:32215]
在Module.new, Class.new中,若給出了塊的話,則在生成的模塊/類的上下文中計算塊的內(nèi)容。
Module.new {|m| p m} => ruby 1.7.1 (2001-10-15) [i586-linux] #<Module:0x401afd5c>
不能對Numeric這種immutable對象進行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不再對數(shù)組進行特殊處理了,而是輸出Array#to_s。Array#to_s在默認情況下會輸出帶換行的字符串,所以從其輸出形式看來是沒有什么變化的(但要受到$,的值的影響)。[ruby-dev:15043]
該修改尚處于試驗階段,又可能會改回原樣。。。[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")都可以清除干凈。
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]
若方法名和括弧之間有空格,則該括號不會為當作是包含參數(shù)的括號,而會被當作表達式中的括號。
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
若對結構體類的子類進行dump的話,則無法讀出。[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>
全局變量的別名無效。 [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
新增第二個可選參數(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 這兩個方法來生成對象。[ruby-dev:14847] 請參考 rb_define_alloc_func() 。
若將數(shù)組傳給Array.new 的參數(shù)時,將會生成該數(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í)行過程中使用Ctrl-C進行中斷之后,還可以恢復終端狀態(tài)。[ruby-dev:14574]
新增(Module#included的重定義)
新增。
while, until, class, def能返回值了。
class/module 返回最后計算的表達式的值。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會在途中返回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
以前在對 字符串的范圍對象 和 字符串 進行比較時,只會與范圍的兩段作比較。而現(xiàn)在則使用String#upto來和每個元素進行比較。
(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而準備的sort。
Updated. New methods and constants for using the mouse, character attributes, colors and key codes have been added.
新增。每隔step就使用對應元素進行迭代。
條件式中的正則表達式字面值 會給出警告。
在于$_ 進行正則表達式匹配時,推薦您這樣顯式地處理 ~/re/ (單項的 ~方法)。
$_ = "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"
新增。去除左端或右端的空格。
新增。套接字地址結構體(INET domain)的pack/unpack。
新增。套接字地址結構體(UNIX domain)的pack/unpack。
新增。忽略字母的大小寫區(qū)別來比較字符串。
不管$=的值如何,總是區(qū)分字母的大小寫。
新增 [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ù)的個數(shù)之外,其他則與Proc#call 相同。
新增
在File::Constants模塊中定義了該方法所用的FNM_NOESCAPE, FNM_PATHNAME, FNM_PERIOD, FNM_CASEFOLD標識。
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ù)比較起來,它應該是正確的。
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] []
修正了下列問題?,F(xiàn)在,只包含1元素的數(shù)組也能被正常展開。
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 的子類中去。類的層次關系如下。
NoMethodError < NameError < StandardError.
以前,只有將第2參數(shù)指定為數(shù)值(File::RDONLY|File::CREAT等)時,才會使用第3參數(shù)?,F(xiàn)在只要給出了第3參數(shù),就總是有效的。[ruby-bugs-ja:PR#54]
由于使用了內(nèi)部的哈希表,所以使用常數(shù)的速度有所提升。(對應于ChangeLog的
Tue Jun 5 16:15:58 2001 Yukihiro Matsumoto <matz@ruby-lang.org>
部分)
下列代碼(請注意p后面的空格)
p ("xx"*2).to_i
不會被解生成
(p("xx"*2)).to_i
而是
p (("xx"*2).to_i)
這樣(這只是試驗性的修改)。
新增?,F(xiàn)在(因為會自動進行數(shù)組變換,所以)可以寫成這樣。(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 的值了。(該功能尚處于試驗階段)
break [n]會終止迭代器,n就是迭代器的返回值。 而next [n]會跳到塊的外面,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的對象將在更大的空間內(nèi)扮演String的角色。
大部分將字符串作為參數(shù)的內(nèi)部方法都在嘗試調(diào)用to_str進行隱式的類型變換。
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]
如果把一個包含to_str方法的非字符串對象傳給擴展庫的API--STR2CSTR()的話,它就會在內(nèi)部調(diào)用to_str,進行隱式的類型轉換。此時雖然能返回變換結果所保持的字符串指針,但在該API中,這個隱式變換的結果不會被任何對象所使用,因此可能會被GC回收掉。 [ruby-dev:12731]
version 1.7以后,使用StringValuePtr()來替代它。此時,參數(shù)所用的對象會被隱式轉換結果所替代,因此變換結果不會被GC所回收。(在version 1.7中,STR2CSTR()變成obsolete)
另外還準備了一個新的API--StringValue()。在需要對參數(shù)進行to_str的隱式類型變換時使用它。若參數(shù)是字符串的話,則不進行任何操作。把它用在處理字符串的方法的前面則會相當方便。
目前還沒有開發(fā)出替代str2cstr() (返回C指針和字符串長度)的安全的API。(在[ruby-dev:15644]中有一些建議)
只有在使用-e選項的單行腳本中,才能對 范圍操作符表達式中的單個數(shù)值字面值 和 $.
進行比較。
使用Module#===來比較 發(fā)生的異常$! 和 rescue中的異常類。
以前使用kind_of?來進行比較時,基本上也沒有什么問題。這里主要是對SystemCallError.===進行了重定義,只要兩個異常的errno相同就把它們看作是同一個異常。因此,若Errno::EWOULDBLOCK 和 Errno::EAGAIN的意義相同(相同的errno)時,不管指定哪個,都可以進行rescue。
后來,只要兩個Errno::XXX對象的errno相同,就被看作是相同的異常,因此這個修改也就失效了,但卻一直保留下來。 (或許在用戶定義異常類時還能用到) [ruby-dev:19589]
在不帶塊的時候,Array#collect會返回self.dup。因此可能會返回Array以外的對象[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 中也進行了修正)
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"可以對nil和NULL指針進行互換了。[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。
不能保證將來一直如此 [ruby-dev:12506]。
(注: 并非Class#inherited)
以前為了禁止類定義子類而設立該方法,(其作用是引發(fā)TypeError異常)現(xiàn)在由Class.new來完成這個任務,所以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相同,塊的結果成為方法的返回值。(在1.6以前,其返回值恒為nil
)
可以指定塊了。
可以使用前面的反斜線來對通配符進行轉義處理。另外,空格也不再具有特殊意義('\0'依然有效)。
新增
新增
新增
新增。主要是實現(xiàn)了[ruby-talk:9460]中提到的功能
Interrupt 成為SignalException的子類。(在1.6以前,它是Exception的子類)
新增。Marshal輸出的dump format的版本號。 [ruby-dev:14172]
新增 [ruby-dev:12766] (2003-03-28: 后來又消失了)
為了方便Regexp#match而設。以前必須得
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]
舊稱被刪除。請使用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還在,但會出現(xiàn)warning)
新增。$?
的值從整數(shù)中分離出來,變成該類的實例。
新增
新增
新增了可選參數(shù)。
新增
新增。比較字符串而不區(qū)分大小寫。
新增
與str[n, 0] = other
相同(但會返回 self)
新增 [ruby-dev:12921]
新增(???之后又被刪除)
新增 (請參考上面的「rescue 節(jié)的...」內(nèi)容) [ruby-dev:12670]
新增
可使用第3,4參數(shù)來進行指定。
新增。返回Thread固有數(shù)據(jù)的鍵的數(shù)組。
可以處理負的 time_t (僅限于OS支持的場合)
p Time.at(-1) => Thu Jan 01 08:59:59 JST 1970
對gmtime 時區(qū)返回UTC" (以前取決于系統(tǒng)環(huán)境,大部分場合返回"GMT")