December 26th, 2007
續前篇。
#instance_methods,#private_instance_methods,#public_instance_methods現在回傳的是 symbol array 而不是 string array。const_defined?, #const_get and #method_defined?增加一個參數,決定要不要判斷 include 進來的部份。比方說:module A; X = 1; def foo; end end module B include A const_defined? "X" # => true method_defined? :foo # => true method_defined? :foo, false # => false const_get "X" # => 1 end
- 新增
class_variable_defined?方法。 class_variable_{set,get}變成 public methods- 新增
attr為 attr_reader 的 alias - Class singleton class:
class X;end; x=X.new; class << x; self < X; end # => true
- Class 變數不會繼承。
- binding evaluation:
a = 1 binding.eval('a') # => 1
Proc加入了yield方法。arity方法的定義更改為參數的個數。proc為Proc.new的簡寫。- 新增
Proc#lambda?方法 - 用 exception 的繼承關係取代掉
StandardError,所以這樣的 code 在 1.9 以後不會被rescue:"".asdas rescue 1
(本來會丟到 stderr 上)
- exceptions 都被視為(比較上)相等的。
SystemStackError<code> 改為繼承自 <code>StandardError。- exception 不再提供
to_str方法,也就是$!.to_str的寫法會有問題。 - Enum 新增
cycle方法,用法如下:a = ["a", "b", "c"] a.cycle {|x| puts x } # print, a, b, c, a, b, c,.. forever.
Enumerable#each_with_index讓原本的each方法可以指定從哪個 index 開始。Enumerable#first(n)傳回前 n 個物件。Enumerable#group_by以 block 的回傳值來分 group。如:(1..10).group_by{|x| x % 3} # => {0=>[3, 6, 9], 1=>[1, 4, 7, 10], 2=>[2, 5, 8]}
Enumerable#find_index根據 block 回傳值來傳回符合的 index:(1..10).find_index{|x| x % 5 == 0} # => 4 (1..10).find_index{|x| x % 25 == 0} # => nil
Enumerable#take若只傳一數字參數,則與first方法相同,否則就根據 block 的回傳值。a = [1, 2, 3, 4, 5] a.take(3) # => [1, 2, 3] a.take {|i| i < 3 } # => [1, 2]
Enumerable#drop與take方法相反。Enumerator#each如果沒有傳入 block ,則傳回自己本身。- 如果對一個 enumerable 物件沒有指定 block 的話,則執行結果傳回一
Enumerable物件。 Enumerable#inject(reduce)若沒傳入 block,則以第一個參數為作用的方法,第二個參數(選擇性)為初始值。Enumerable#count方法,計算 enum 裡有幾個符合 block 或參數條件的物件:["bar", 1, "foo", 2].count(1) # => 1 ["bar", 1, "foo", 2].count{|x| x.to_i != 0} # => 2
Enumerator#with_index讓 enumerables 在 iterate 時提供 index:[1,2,3,4,5,6].map.with_index {|x,i|[2,5].include?(i) ? x : x*2} # => [2, 4, 3, 8, 10, 6]
Enumerable##min_by, #max_by方法找出最小、最大值。Enumerable#zip方法不再將參數轉型成 array。
文章分類:
標籤:

