精華區beta Ruby 關於我們 聯絡資訊
出自我的Blog http://lightyror.blogspot.com/2006/09/ruby_21.html 看到railscn這一篇 http://www.railscn.com/viewtopic.php?p=10560#10560 裡面有相當多 Ruby 的小技巧,讓你 coding 比較 Ruby Way~~ 我列出一些很好用的 1. for i in (1..10) puts i end 可以這樣寫 (1..10).each{|i| puts i} 或 1.upto(10){|i| puts i} 2. number = 1 if number.nil? number = 1 unless number 可以這樣寫 number ||= 1 3. result = [] (1..10).each{|item| result <<> 可以這樣寫 (1..10).inject([]){|array, item| array <<> 極有用~~~ 4. 平行Assignment a,b = 0, 1 a,b = b, a 5. a, *b = [1,2,3] #a = 1, b = [2,3] 6. begin 1/0 rescue puts 'wrong' end 可以變成 1/0 rescue puts 'wrong' 7. opt中同樣Key 的內容會覆蓋default_opt中key的value def image(opt={}) default_opt = {:height => 25, :width => 10} default_opt.merge! opt end 大家可以繼續來接力~~~ -- lighty RoR 是一個介紹 lighttpd , SQLite , Ruby and Rails 的 Blog http://lightyror.blogspot.com/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.230.119.234 ※ 編輯: giive 來自: 61.230.119.234 (09/21 20:50)
PttHuge:很多都是grammar candy @_@ 09/22 01:12