Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby on Rails | Rails Consoleをirbからpryに変更する

概要

Rails Consoleをirbからpryに変更する

内容

irbの「強化版」pryをRails Consoleで利用するには以下をGemfileに追加します。

gem "pry-rails", "~> 0.3.1"

設定後bunlderの実行

bundle

設定確認

Rails Console起動

bundle exec spring rails c

pryの機能が動作していることを確認します。
数値型変数を宣言して、cdで変数内に移動。
lsでメソッド一覧を表示したり、
show-methodでNumericクラスがRailsによって
拡張されて、megabyteメソッドなどが追加されていることを確認できます。
※また、irbと異なりpry色付き強調表示されます。

[8] pry(main)> hoge = 1
=> 1
[9] pry(main)> cd hoge
[10] pry(1):1> ls
Comparable#methods: between?
Numeric#methods: 
  +@         days         html_safe?  phase               singleton_method_added
  abs2       duplicable?  i           polar               step                  
  ago        encode_json  imag        pretty_print        terabyte              
  angle      eql?         imaginary   pretty_print_cycle  terabytes             
  arg        exabyte      kilobyte    quo                 to_c                  
  as_json    exabytes     kilobytes   real                to_formatted_s        
  blank?     fortnight    megabyte    real?               until                 
  byte       fortnights   megabytes   rect                week                  
  bytes      from_now     minute      rectangular         weeks                 
  coerce     gigabyte     minutes     remainder         
  conj       gigabytes    nonzero?    second            
  conjugate  hour         petabyte    seconds           
  day        hours        petabytes   since             
Integer#methods: 
  ceil         gcd       months        ordinal      times   to_r    
  chr          gcdlcm    multiple_of?  ordinalize   to_bn   truncate
  denominator  integer?  next          pred         to_d    upto    
  downto       lcm       numerator     rationalize  to_i    year    
  floor        month     ord           round        to_int  years   
JSON::Ext::Generator::GeneratorMethods::Fixnum#methods: to_json
Fixnum#methods: 
[16] pry(1):1> show-method Numeric#megabyte

From: /home/user/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/core_ext/numeric/bytes.rb @ line 20:
Owner: Numeric
Visibility: public
Number of lines: 3

def megabytes
  self * MEGABYTE
end
[17] pry(1):1> 

サンプル画像