環境チェックに使えそうなメソッドたち

コンソールから色々調べたい時には
ruby script/console


以下、環境の確認に便利なメソッドたち。

Rails::Info

Railsの動作環境を表示。
Welcome aboard画面のリンクやruby script/aboutと同じ結果が返る。


しかしこれ、InfoってModuleクラスなのになんでこんなのが出てくるんだろう?
実際の値はRails::Info.propertiesに格納されているのは分かるんだけれども…。

Ruby version 1.8.5 (i386-mswin32)
RubyGems version 0.9.2
Rails version 1.2.2
Active Record version 1.15.2
Action Pack version 1.13.2
Action Web Service version 1.2.2
Action Mailer version 1.3.2
Active Support version 1.4.1
Application root **
Environment development
Database adapter mysql
Database schema version 1

ActiveRecord::Base.configurations

database.ymlの中身をDumpする。

pp ActiveRecord::Base.configurations
{"development"=>
{"username"=>"root",
"adapter"=>"mysql",
"host"=>"localhost",
"password"=>"root",
"database"=>"hellorails_development"},
"production"=>
{"username"=>"root",
"adapter"=>"mysql",
"host"=>"localhost",
"password"=>"root",
"database"=>"hellorails_production"},
"test"=>
{"username"=>"root",
"adapter"=>"mysql",
"host"=>"localhost",
"password"=>"root",
"database"=>"hellorails_test"}}

RAILS_ENV

現状の動作モードが分かる。

RAILS_ENV
=> "development"

ActiveRecord::Base.connection.pretty_inspect

なんか色々出てくる。
コマンドプロンプトだと見辛かったんでファイルに出力した。

File.open('c:\temp.txt','w'){|f| f.write ActiveRecord::Base.connection.pretty_inspect}

#"mysql",
:database=>"hellorails_development",
:password=>"root",
:host=>"localhost",
:allow_concurrency=>false,
:username=>"root"},
@connection=#,
@connection_options=
["localhost", "root", "root", "hellorails_development", nil, nil],
@last_verification=0,
@logger=
#,
@formatter=nil,
@level=0,
@logdev=
#,
@filename="./script/../config/../config/../log/development.log",
@mutex=
#,
@mon_owner=nil,
@mon_waiting_queue=>,
@shift_age=0,
@shift_size=1048576>,
@progname=nil>,
@runtime=0.062000036239624>

ActiveRecord::Base.connection.pretty_print_instance_variables

pp ActiveRecord::Base.connection.pretty_print_instance_variables
["@config",
"@connection",
"@connection_options",
"@last_verification",
"@logger",
"@runtime"]

これRailsに限ったメソッドではないみたい。
Objectクラスのインスタンスメソッドだからどのオブジェクトからも呼べる。

ActiveRecord::Base.connection.current_database

"hellorails_development"