Ruby on Rails 中的 IRB、Pry、rails console 差別

被端走的小菜
7 min readNov 25, 2019

--

本文章同步發表於小菜的 Blog https://riverye.com/

說明

欲測試想法(但懶得開新檔案)或抓 bug 時,最常使用的工具,將針對 irbpryrails console 進行介紹。

IRB

Ruby 內建一個名為 IRB (Interactive Ruby) 的互動式命令列,可以在裡面輸入語法且立即看到結果,只需在終端機輸入 irb 即可進入。(前提要有安裝 Ruby)

# 在終端機輸入
$ irb

> class Human
> def going_to_travel
> "好想出國旅遊"
> end
> end
=> :going_to_travel
> Human.new.going_to_travel
=> "好想出國旅遊"

# # 為註解
# $ 為終端機輸入 ($ 免輸入)
# > 為 IRB 介面 (> 免輸入)
# => 為回傳值

像這樣的環境也稱為 REPL (Read-Eval-Print-Loop)(讀取-執行-印出-循環)。

IRB 使你可以在終端機介面快速執行並測試回傳值,當你遇到錯誤時, IRB 會準確告訴你該錯誤是什麼及該錯誤的位置。

$ irb

> class Human
> def going_to_travel
> "好想出國旅遊"
> end
> end
=> :going_to_travel
> Human.new.go_to_travel

NoMethodError (undefined method `go_to_travel' for #<Human:0x00007fffd74be090>)
Did you mean? going_to_travel

在這裡我遇到了 NoMethodError,因為我使用錯誤的方法名稱,我把 going_to_travel 換成 .go_to_travel ,它會準確告訴我問題出在哪裡,Ruby 甚至會把它找到與輸入內容接近的方法給我參考,有沒有覺得 Ruby 實在太強大了!! 感謝 Ruby ,讚嘆 Ruby。

要退出 IRB 很簡單,只需輸入 exitquitCtrl + D ,即退回終端機介面。

盡管 IRB 非常適合快速檢查某些內容,但它不允許在方法或循環中進行測試,此時用 Pry 較適合。

Pry

Pry 是 IRB 的加強版,有大量附加的功能(ex: 更漂亮的介面、可以使用 show-method 等)。雖說 Pry 更加強大,但蠻多時候,我仍以 IRB 進行測試,直到需要在 classes, methods, hashes, iterations 中進行測試時,Pry 會是你的好夥伴。

安裝 Pry

可以透過安裝取得 Pry

# 終端機輸入
$ gem install pry

或在檔案中加入 require 'pry',並使用 binding.pry 該方法可以立即將程式碼暫停,以便進行測試。會從 binding.pry 這裡開始,並啟動新的 REPL 介面,只需在 Pry 中測試,無須猜測該程式碼的回傳值是什麼。

# test-pry.rb
require 'pry'

class A
def hello()
puts "hello world!"
end
end

a = A.new

# start a REPL session
binding.pry

# program resumes here (after pry session)
puts "program resumes here."
# 終端機輸入
$ ruby test-pry.rb

From: /mnt/c/code/demo/Ruby/test-pry.rb @ line 12 :

7: end
8:
9: a = A.new
10:
11: # start a REPL session
=> 12: binding.pry
13:
14: # program resumes here (after pry session)
15: puts "program resumes here."

[1] pry(main)> a.hello
hello world!
=> nil
[2] pry(main)> def a.abc
[2] pry(main)* puts "在 pry 中測試,並不會改變原本 test-pry.rb 檔案"
[2] pry(main)* end
=> :abc
[3] pry(main)> a.abc
在 pry 中測試,並不會改變原本 test-pry.rb 檔案
=> nil
[4] pry(main)> exit
program resumes here.

也可以直接在終端機輸入 pry 進入操作

# 終端機輸入
$ pry

要退出 Pry 並繼續執行後面程式碼的話,只需輸入 exitquitCtrl + D

退出 Pry 並中止後面程式碼執行,輸入 !!!

從 IRB 進 Pry

要從 IRB 中進入 Pry 的話,先輸入 require 'pry' 再輸入 binding.prypry 即進入 Pry。

# 終端機輸入
$ irb

> require 'pry'
=> true

# 方法 1:
> binding.pry
[1] pry(main)> exit
=> nil

# 方法 2:
> pry
[1] pry(main)> quit
=> nil

從 Pry 進 IRB

要從 Pry 中進入 IRB 的話,輸入 binding.irb 即可。

Rails console

類似 Ruby 的 IRB 介面,並載入整個 Rails 專案的環境,可以在這裡直接操作資料,快速驗證想法且不用到網站便能修改伺服器上的資料:

# 要在 Rails 專案資料夾底下
$ rails console
# 縮寫為 rails c

> t = Task.find(1) # 括號可省略
> t.title
=> "看書"
> t.title = "跑步 30 分鐘"
=> "跑步 30 分鐘"
> t.title
=> "跑步 30 分鐘"

預設 rails c 是進入 development 環境,若想進入 production 或 test 環境,只要在 rails c 後面加入對應單字即可:

# Loading development environment
$ rails c

# Loading production environment
$ rails c production

# Loading test environment
$ rails c test

若只想測試,但不想修改資料,可以使用:

$ rails c --sandbox

Loading development environment in sandbox (Rails 5.2.3)
Any modifications you make will be rolled back on exit

要退出 rails console 的話,只需輸入 exit 或 quit 或 Ctrl + D 。

用 Pry 執行 rails c

在 Rails 中,如果不想更改 Gemfile ,可以使用 pry -r 在應用程序環境中運行 Pry:

$ pry -r ./config/environment

安裝 Gemfile 的話,使用「 pry-rails」gem

gem 'pry-rails', :group => :development

小結

這三個 irbpryrails console 沒有好壞之分,依不同情境需求,選擇不同工具處理即可。

--

--

被端走的小菜
被端走的小菜

Written by 被端走的小菜

大家好,我是被端走的小菜。以個人部落格更新為主:https://riverye.com/

No responses yet