顯示具有 Ruby 標籤的文章。 顯示所有文章
顯示具有 Ruby 標籤的文章。 顯示所有文章

2013年5月7日 星期二

Mac Install RVM

用RVM管理Ruby version

RVM Install
curl -L https://get.rvm.io | bash -s stable --rails --autolibs=enable
rvm get stable --autolibs=enable

安裝想安裝的版本
rvm install 2.0

安裝完後可以從下面查裝的版本
rvm list rubies

安裝完後在執行的時候還是舊的版本,你還需要去指定成新的版本
rvm use 2.0.0 --default

這樣就完成了

2013年1月2日 星期三

Rubular

在Ruby很常會使用到 Regular

這個網站可以針對你的需要處理的字串,透過你所寫的RegExp馬上顯示出來所截取的資料


Ruby Regular Expressions


我們從這個例子
# 抓出手機號碼 
phone = "123-456-7890"
if phone =~ /(\d{3})-(\d{3})-(\d{4})/
  ext  = $1
  city = $2
  num  = $3
end

可以抓出分別是123, 456, 7890

但在新的一版1.9可以在正規表示內加入參數
# 抓出手機號碼 
match_phone =  /(?<ext>\d{3})-(?<city>\d{3})-(?<num>\d{4})/.match(phone)
puts match_phone[:ext]
puts match_phone[:city]
puts match_phone[:num]

參考資料
ihower
http://www.igvita.com/2011/02/03/new-ruby-19-features-tips-tricks/