精華區beta Ruby 關於我們 聯絡資訊
出自我的Blog http://lightyror.blogspot.com/2006/10/irb.html 剛剛看到 JavaEye 這一篇 強化irb,感到十分的興奮。特別寫出來,感謝對岸的cookoo先生。以下功能在 Linux 下面測試,均可直接套用到 ruby script/console 。 安裝 1. 安裝下列套件 wirble, map_by_method, what_methods gem i wirble map_by_method what_methods 2. 寫入到 home 底下的 .irbrc 下面 # Compiled by Cookoo # Reference: # http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb # http://pablotron.org/software/wirble/ require 'rubygems' require 'map_by_method' require 'what_methods' require 'wirble' require 'irb/completion' IRB.conf[:AUTO_INDENT]=true class Regexp def show(a) a =~ self ? "#{$`}<<#{$&}>>#{$'}" : "no match" end end Wirble.init Wirble.colorize unless Config::CONFIG['host_os'] == 'mswin32' 然後就可以開始使用了 我們發現到有許多新功能(我只列出我覺得很有用的功能) 1. irb 有顏色了 2. 歷史紀錄(原本的irb 也有,但是跳出irb,再重新進入irb 就不能叫出以前的紀錄了) 3. tab auto-complete 4. 直接可以使用ri查詢doc 5. regular expression 快速查詢 6. 可以使用 pp 7. .what? 猜相關的 api 方式 1. 使用ri >> ri 'String' ---------------------------------------------------------- Class: String A +String+ object holds and manipulates an arbitrary sequence of bytes, typically representing characters. String objects may be created using +String::new+ or as lite.... 2. reaular expression 快速查詢 他會將這個regular expression match 的部份用 << >> 標起來 >> /[a-z]+/.show "pi is 3.14" => "<<pi>> is 3.14" 3. .what? 猜api 一個相當不賴的功能, a.what? b 他會列出有a所有 method 當中,所有可能讓 a 變成 b的 api >> 3.14.what? 3 3.14.floor == 3 3.14.to_int == 3 3.14.prec_i == 3 3.14.round == 3 3.14.to_i == 3 3.14.truncate == 3 => ["floor", "to_int", "prec_i", "round", "to_i", "truncate"] -- lighty RoR 是一個介紹 lighttpd , SQLite , Ruby and Rails 的 Blog http://lightyror.blogspot.com/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.230.98.239 ※ 編輯: giive 來自: 61.230.98.239 (10/20 16:27)
PttHuge:喔喔..希望irb跟vim一樣強大 XD 10/20 17:04
PttHuge:不過那個 .what? 想不到有什麼機會會用到 ~_~ 10/20 17:05
> -------------------------------------------------------------------------- < 作者: giive (lala) 看板: Ruby 標題: Re: 強化你的 irb 時間: Fri Oct 20 17:18:43 2006 ※ 引述《giive (lala)》之銘言: : 推 PttHuge:喔喔..希望irb跟vim一樣強大 XD 10/20 17:04 : → PttHuge:不過那個 .what? 想不到有什麼機會會用到 ~_~ 10/20 17:05 其實沒啥太大的用處,炫倒是很炫啦 昨天我朋友問我(他是Ruby on Rails 新手) ㄟ,我有一個 Range 1..6 要怎麼把他變成 Array 這時候 >> b = 1..6 => 1..6 >> b.what? [1,2] 1..2.map == [1, 2] 1..2.sort == [1, 2] 1..2.to_a == [1, 2] 1..2.collect == [1, 2] 1..2.entries == [1, 2] => ["map", "sort", "to_a", "collect", "entries"] 應該有點用 XD -- lighty RoR 是一個介紹 lighttpd , SQLite , Ruby and Rails 的 Blog http://lightyror.blogspot.com/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.230.98.239 ※ 編輯: giive 來自: 61.230.98.239 (10/20 17:20)
PttHuge:這實例突然覺得還蠻有用的, good to drink 10/20 17:27