[Ruby] ruby 1.9 new features

看板Ruby作者 (godfat 真常)時間17年前 (2007/12/06 01:10), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/4 (看更多)
當然最有名的就是這篇文章了: http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9 稍微試了一下感覺: > New literal hash syntax [Ruby2] {a: "foo"} # => {:a=>"foo"} 1.9 可用。 > Block local variables [EXPERIMENTAL] 他的 ; 隔開的 local variables 我不知道意義何在, 試了各種寫法後我的結論是現在永遠會 shadow outer variables... 這樣就不能做一些比較 tricky 的東西,不過也好, 其實 ruby 1.8 這樣的行為滿容易出意外的... > Block arguments are always local 那要那 ; 幹嘛? > New syntax for lambdas [VERY EXPERIMENTAL] 這個實在是...有點好笑啦,因為能寫出很莫名其妙的 expression... ->a,*b,&c;d{c[a,b]}.(7,8){|*a|a} # => [7, [8]] 這樣會沒人看得懂吧 @_@b > .() and calling Procs without #call/#[] [EXPERIMENTAL] 其實就是轉呼叫 call method... class Test def call test test+1 end end Test.new.(10) # => 11 > Blocks can take &block arguments: finally!! 早該有的。 > News semantics for block arguments 這也是早該是這樣的,就不多談了。 > Method used for splat arguments: #to_splat 沒這個東西,大概還是叫 to_a > Multiple splats allowed 唐大師的建議? XD 非常方便的東西,簡單地說就是哪裡都能用 splats [0, *[1, 2], 3, *[4, 5]] # => [0, 1, 2, 3, 4, 5] 當然 parameter 上也是,沒有會讓人意外的東西所以就不示範了。 > Mandatory arguments after optional arguments allowed 這是我覺得很恐怖的怪東西 :( def f a, b=nil, *c, d [a, b, c, d] end f 1,2 # => [1, nil, [], 2] f 1,2,3 # => [1, 2, [], 3] f 1,2,3,4 # => [1, 2, [3], 4] f 1,2,3,4,5 # => [1, 2, [3, 4], 5] f 1,2,3,4,5,6 # => [1, 2, [3, 4, 5], 6] a 永遠會是 第一個 arg d 永遠會是最後一個 arg b 如果超過 2 個 arg, 則會是第二個 arg, 否則是 default value c 永遠會是剩下的全部 arg... 很複雜...。 > #instance_exec 可以傳 args 的 instance_eval > send doesn't always call private methods anymore (#__send, #__send!) 騙人,明明就可以,而且也沒有 __send, __send!, send! 所以現在是維持 1.8 不改? > Object#tap 讓你把東西全部串在一起的好工具,facets 其實也有。 待續... -- 生死去来、棚頭傀儡、一線断時、落落磊磊 《花鏡》-世阿弥 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.132.58.12

12/06 02:30, , 1F
推!
12/06 02:30, 1F
文章代碼(AID): #17LjkJvW (Ruby)
討論串 (同標題文章)
文章代碼(AID): #17LjkJvW (Ruby)