/RailsApp/public/dispatch.fcgi No such file or directory とエラーが出るが実際にはあるんだが・・・

以下のようなエラーが出る

[fuga@hogehoge RailsApp]$ sudo /etc/init.d/lighttpd start
Starting lighttpd:                                         [  OK  ]
[fuga@hogehoge RailsApp]$ 2009-05-14 15:20:36: (mod_fastcgi.c.1022) execve failed for: /RailsApp/public/dispatch.fcgi No such file or directory 
2009-05-14 15:20:36: (mod_fastcgi.c.1048) the fastcgi-backend /RailsApp/public/dispatch.fcgi failed to start: 
2009-05-14 15:20:36: (mod_fastcgi.c.1052) child exited with status 2 /Rails_Ap/public/dispatch.fcgi 
2009-05-14 15:20:36: (mod_fastcgi.c.1055) if you try do run PHP as FastCGI backend make sure you use the FastCGI enabled version.
You can find out if it is the right one by executing 'php -v' and it should display '(cgi-fcgi)' in the output, NOT (cgi) NOR (cli)
For more information check http://www.lighttpd.net/documentation/fastcgi.html#preparing-php-as-a-fastcgi-program 
2009-05-14 15:20:36: (mod_fastcgi.c.1060) If this is PHP on Gentoo add fastcgi to the USE flags 
2009-05-14 15:20:36: (mod_fastcgi.c.1356) [ERROR]: spawning fcgi failed. 
2009-05-14 15:20:36: (server.c.834) Configuration of plugins failed. Going down. 


「dispatch.fcgi No such file or directory」 - dispatch.fcgi は /rails_app/public/dispatch.fcgi に存在しており、実行権もある。



dispatch.fcgi を直接実行してみた。

$ ./dispatch.fcgi 
-bash: ./dispatch.fcgi: /usr/bin/ruby18: bad interpreter: No such file or directory

どうも、 /usr/bin/ruby18 がないと怒っているらしい。

たしかに、/usr/bin/ruby18 は存在しない。ということで、シンボリックリンクを作成する。

sudo ln -s /usr/local/bin/ruby /usr/bin/ruby18


起動してみる。

$ sudo /etc/init.d/lighttpd start
Starting lighttpd:                                         [  OK  ]

無事起動しました。

たのしいRuby(第2版) 「はじめてのRuby」〜別のファイルを取り込む〜


■これは「たのしいRuby 第2版」を元にしています。
http://www.amazon.co.jp/%E3%81%9F%E3%81%AE%E3%81%97%E3%81%84Ruby-%E7%AC%AC2%E7%89%88-Ruby%E3%81%A7%E3%81%AF%E3%81%98%E3%82%81%E3%82%8B%E6%B0%97%E8%BB%BD%E3%81%AA%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0-%E9%AB%98%E6%A9%8B-%E5%BE%81%E7%BE%A9/dp/4797336617/ref=pd_sim_b_img_6/250-4677527-6737860

別のファイルを取り込む

 プログラムの一部を、別の新しいプログラムの中で使いまわしたいことがあります。たとえば、あるプログラムで使った自作メソッドを、別のプログラムで利用したい、といった場合です。
 たいていのプログラミング言語では、別々のファイルに分割されたプログラムを組み合わせて、1つのプログラミングとして利用するための機能を持っています。他のプログラムから読み込んで利用するためのプログラムを、ライブラリといいます。
 プログラムの中でライブラリを読み込むためには、requireメソッドを使います。


require "使いたいライブラリのファイル名"


使いたいライブラリのファイル名の「.rb」は省略することができます。
requireメソッドを呼ぶと、Rubyは引数に指定されたライブラリを探して、そのファイルに書かれた内容を読み込みます。ライブラリの読み込みが終わると再び、requireメソッドの次の行から処理を再開します。

では、やってみましょう。

def hello
  print("「う…うろたえるんじゃあないッ! ドイツ軍人はうろたえないッ!」")
end
require "jojo"
hello()
  • 実行例
C:\enjoy_ruby>ruby user_jojo.rb
「う…うろたえるんじゃあないッ! ドイツ軍人はうろたえないッ!」

おおぉ。表示されましたね!


 Rubyには、たくさんの便利なライブラリが標準で付属しています。これらを利用する場合にもrequireメソッドを使います。たとえば、日付を扱うDateモジュールは


require "date"


とすることで利用できるようになります。


プログラミング言語 Ruby リファレンスマニュアル

せっかくなので、1つメソッドを使ってみましょう。

  • helloruby.rb
require "date"
p Date.new
p Date::MONTHNAMES
  • 実行結果
C:\enjoy_ruby>ruby helloruby.rb
#<Date: -1/2,0,2299161>
[nil, "January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November","December"]

ちゃんと使えましたね。


ただ、Dateクラスは組み込みクラスなので、実際はrequireしなくても使えます。


プログラミング言語 Ruby リファレンスマニュアル


環境

C:\enjoy_ruby>ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]

たのしいRuby(第2版) 「はじめてのRuby」〜制御構造 繰り返し2〜


■これは「たのしいRuby 第2版」を元にしています。
http://www.amazon.co.jp/%E3%81%9F%E3%81%AE%E3%81%97%E3%81%84Ruby-%E7%AC%AC2%E7%89%88-Ruby%E3%81%A7%E3%81%AF%E3%81%98%E3%82%81%E3%82%8B%E6%B0%97%E8%BB%BD%E3%81%AA%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0-%E9%AB%98%E6%A9%8B-%E5%BE%81%E7%BE%A9/dp/4797336617/ref=pd_sim_b_img_6/250-4677527-6737860


繰り返しのメソッドについて追加です。
前回は、
たのしいRuby(第2版) 「はじめてのRuby」〜制御構造 繰り返し〜 - 基本へ帰ろう
です。

loop

 終了条件がない、ただの繰り返しのためのメソッドもあります。loopメソッドです。


loop {
print "「う…うろたえるんじゃあないッ! ドイツ軍人はうろたえないッ!」"
}

上記のプログラムを実行すると、
"「う…うろたえるんじゃあないッ! ドイツ軍人はうろたえないッ!」j"
がコンピュータの資源が許す限り、ずっと出力されることになりますのでやりませんが、
これが、loopというものですね。


繰り返しの制御

 繰り返しの途中で、処理を中断したり、次の回に処理を飛ばしたりしたいことがあります。Rubyには繰り返しやイテレータを制御する3つの命令があります。

break 繰返しを中断し、繰り返しの中から抜ける。
next 次の回の繰り返しに処理を移す。繰り返しの中のnext以降の部分を飛ばして、次の回の処理を開始します。
redo 同じ条件で繰返しをやり直す。redoはnextと似ていますが、もう一度同じ繰返しを実行するところが違います。

breakとnextとredoの違い

具体的にコードを書いてみましょう。

  • プログラム
print "breakの例:\n"
i = 0
["Perl", "Python", "Ruby", "Scheme", "Lips"].each{|lang|
 i += 1
 if i == 3
   break
 end
 p [ i, lang]
}


print "nextの例:\n"
i = 0
["Perl", "Python", "Ruby", "Scheme", "Lips"].each{|lang|
 i += 1
 if i == 3
   next
 end
 p [ i, lang]
}


print "redoの例:\n"
i = 0
["Perl", "Python", "Ruby", "Scheme", "Lips"].each{|lang|
 i += 1
 if i == 3
   redo
 end
 p [ i, lang]
}
  • 実行結果
C:\enjoy_ruby>ruby helloruby.rb
breakの例:
[1, "Perl"]
[2, "Python"]
nextの例:
[1, "Perl"]
[2, "Python"]
[4, "Scheme"]
[5, "Lips"]
redoの例:
[1, "Perl"]
[2, "Python"]
[4, "Ruby"]
[5, "Scheme"]
[6, "Lips"]

まとめ

 繰り返しの機能そのものだけを考えるとすれば、while文があればどんな繰り返しでも実現できます。極端なことをいえば、ほかの繰り返しの構文やメソッドは必要ないわけです。それにも関らず、繰り返しのための道具がこんなにも揃っているのは、プログラムが単に機能を実現するためのものではなく、書く人にとっても読む人にとっても、わかりやすくすることが大切だから、といえるでしょう。
最初に紹介した各メソッド・構文の一覧に、使いこなすための指針として「主な用途」を追加した表を紹介しておきます。

構文 メソッド 主な用途
times 回数の指定された繰り返しに便利
for eachのシンタックスシュガー
while 条件を自由に指定したい場合
until whileではわかりにくい条件を指定したい場合
each オブジェクトから要素を取り出す場合
loop 回数制限のない繰り返しに便利

環境

C:\enjoy_ruby>ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]

File.openとFile.writeを使った画像のコピー(読み込み&書き込み)

what

File.openとFile.writeを使ったコピーで、テキストファイルはうまくいくが、画像ファイルがうまくいかないので、うまくいくようにする。

以下のファイルがあります
C:\enjoy_ruby\file\file.rb ※rubyファイル
C:\enjoy_ruby\file\original.jpg ※適当な画像ファイル 21KB
Rubyファイル
original = File.open("original.jpg", "r") # 読み込みモード
copy     = File.open("copy.jpg", "w") # 新規作成書き込みモード
copy.write(original.read)
original.close
copy.close

上記を実行すると、エラーは出ません。
結果は、copy.jpg はできるのですが、1KBしかなく画像になっていません。

ためしに、画像ファイルではなく、テキストファイルでやってみたらちゃんとコピーされました。
画像の場合は何が違うのでしょうか。

バイナリモードでやってみる

original = File.open("original.jpg", "r+b") # 読み込みモード
copy     = File.open("copy.jpg", "w+b") # 新規作成書き込みモード
copy.write(original.read)
original.close
copy.close

あっさり成功w
画像はバイナリファイルなので、バイナリモードでやらないとってことですね。

ちなみに

copy.jpg 側だけを通常モードでオープンすると、ファイルサイズは同じくらいになりますが、画像として成立しません。

original = File.open("original.jpg", "r+b") # 読み込みモード
copy     = File.open("copy.jpg", "w") # 新規作成書き込みモード
copy.write(original.read)
original.close
copy.close
オリジナル画像


コピーした画像

※ちなみに、 copy.jpg のままではエラーがおきて、はてなフォトライフにはアップできませんでした。画像ファイルとしてはバイナリが壊れているようですね。上記の写真はローカルマシンでプレビューしたイメージをPrint Screenで切り取ったものです。


カピストラーノ(Capistrano)を使ってみよう!(Basic編)

環境

とりあえず、実行する環境を提示します。

  • クライアント側

OS : Windows XP
Ruby : 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]

  • サーバー側

OS : Linux
 $ cat /etc/redhat-release
 Red Hat Enterprise Linux ES release 4 (Nahant Update 4)
SSH : OpenSSH
 $ ssh -v
 OpenSSH_4.5p1, OpenSSL 0.9.7m 23 Feb 2007

カピストラーノ(Capistrano)って何?

Railsのデプロイに特化しているので、デプロイツールと思われますが、SSHでアクセスでアクセスして、シェルでうにょうにょやれるので、デプロイに限らず色々できます。
クライアント側からシェルが実行できますので、Linux上で書くシェルファイルを、クライアント側(たとえばwindowsとか)でかけて、それをRubyを使って実行できると思ってもよいかもしれません。


EURO 2016 News
↑やっぱ、本家を見るのが一番!ということで、探検です。

いきなり、

Simple.The way it should be.

と書いてあります。インストールや運用も簡単であってほしいと願いつつ続きを読みます。

・Great for automating tasks via SSH on remote servers, like software installation, application deployment, configuration management, ad hoc server monitoring, and more.

どうやら、クライアントからサーバーにSSHでアクセスして、うにょうにょやるようです。

・Ideal for system administrators, whether professional or incidental.

むむん。理想・・・。

・Easy to customize. Its configuration files use the Ruby programming language syntax, but you don't need to know Ruby to do most things with Capistrano.
・Easy to extend. Capistrano is written in the Ruby programming language, and may be extended easily by writing additional Ruby modules.

Easyと言っていますが・・・Easyであることを願います。


install

gem install -y capistrano

-y オプション

無条件に必要な依存するRubyをインストールしてください。

net-ssh や net-sftp なども使うようなので、net-ssh や net-sftpのバージョンが変わったり、インストールされるとアプリが動かなくなる>< みたいな環境の人は -y オプションは付けないで、すべて手動でインストールするとよいでしょう。

  • インストール
C:\>gem install -y capistrano
Need to update 8 gems from http://gems.rubyforge.org
........
complete
Successfully installed capistrano-2.4.3
Successfully installed net-ssh-2.0.3
Successfully installed net-sftp-2.0.1
Successfully installed net-scp-1.0.1
Successfully installed net-ssh-gateway-1.0.0
Successfully installed highline-1.4.0
Installing ri documentation for capistrano-2.4.3...
Installing ri documentation for net-ssh-2.0.3...
Installing ri documentation for net-sftp-2.0.1...
Installing ri documentation for net-scp-1.0.1...
Installing ri documentation for net-ssh-gateway-1.0.0...
Installing ri documentation for highline-1.4.0...
Installing RDoc documentation for capistrano-2.4.3...
Installing RDoc documentation for net-ssh-2.0.3...
Installing RDoc documentation for net-sftp-2.0.1...
Installing RDoc documentation for net-scp-1.0.1...
Installing RDoc documentation for net-ssh-gateway-1.0.0...
Installing RDoc documentation for highline-1.4.0...

インストール完了!

  • 確認
C:\>gem list

*** LOCAL GEMS ***
 ・
 ・
 ・
capistrano (2.4.3)
   Capistrano is a utility and framework for executing commands in
   parallel on multiple remote machines, via SSH.
 ・
 ・
 ・

Getting Started

Capistrano was originally written to ease the pain of deploying Rails applications. Although it can be (and is) used for a lot more than that now, Rails application deployment is still the primary way that most people use Capistrano, at least initially.
As a result, this “Getting Started” guide will actually be multiple guides in one. If you feel there is an essential guide missing from this section, please email jamis at 37signals dot com and let him know what you think needs to be added.

Railsのデプロイの辛さを緩和するために、つくられたものらしいが、他にもいろいろ使えるよ!って書いてあると思う。


Assumptions

Capistrano makes a few assumptions about your servers. In order to use Capistrano, you will need to comply with these assumptions:

カピストラーノを使うにはいくつかの前提がひつようだよ!って書いてあると思う。

You are using SSH to access your remote machines. Telnet and FTP are not supported.

SSHでアクセスできないとダメ!ってことですね。 TelnetFTPはサポートしていないとさ。

Your remote servers have a POSIX-compatible shell installed. The shell must be called “sh” and must reside in the default system path.

シェルは sh として呼べて、デフォルトのシステムパスにいないといけませんか・・・。デフォルトでいじってないので大丈夫だと思う。

If you are using passwords to access your servers, they must all have the same password. Because this is not generally a good idea, the preferred way of accessing your servers is with a public key. Make sure you’ve got a good passphrase on your key.

パスワード認証であれば、サーバにアクセスする際、同じパスワードを使わなければならない。
とりあえず、パスワード認証が設定されているので、パスワード認証でいろいろ試してから、公開鍵認証に移行します。

You should be comfortable working from a command-line. You do not need to know advanced shell scripting techniques or anything like that (though it helps, if you want to start writing complex Capistrano recipes), but you should be able to navigate directories and execute commands from the command-line. Capistrano has no GUI interface; it is entirely command-line driven.

カピストラーノにはGUIで操作できるインターフェースがない、コマンドラインからディレクトリに行ってコマンドを事項出来るべきです。・・・てきな感じだと思う。

To take full advantage of Capistrano, you should be comfortable (or at least, minimally familiar) with the Ruby programming language. When you write your own Capistrano tasks, you do so in Ruby.

カピストラーノを最大限に利用するために、あなたはRubyプログラミング言語を使えるべきです。カピストラーノタスクを書く時に、あなたはRubyでそうします。


Capfiles

Capistranoはcapfileからを指示を読みます。
Capfilesはただ簡単なテキストファイルです、どんなテキストエディタでも使用できます。
ファイル名は何でもかまいません。

・・・的な感じだと思う。

A Simple example

task :search_libs, :hosts => "www.capify.org" do
  run "ls -x1 /usr/lib | grep -i xml"
end

これはなんでしょうか?

This defines a single task, called “search_libs”, and says that it should be executed only on the “www.capify.org” host. When executed, it will display all files and subdirectories in /usr/lib that include the text “xml” in their name. By default, “run” will display all output to the console.

ようするに、linuxssh でログインして、 ls -x1 /usr/lib | grep -i xml というコマンドを打って、その結果を出力するという感じのようです。

リモート操作する予定のサーバーにSSHでアクセスして、上記のコマンドを実行してみました。

  • Linux上で普通にコマンドを打ってみる
[hoge@fuga ~]$ ls -x1 /usr/lib | grep -i xml
libxml2.a
libxml2.so
libxml2.so.2
libxml2.so.2.6.27
xml2Conf.sh

こんなんでました、カピストラーノでこのような出力を取得できるのでしょうか?

Assuming your capfile is in the current directory, you would execute that task like this (from the command-line):

カレントディレクトリの中にあなたのcapfileがあると仮定する場合、あなたはこの(コマンドラインからの)ようにそのタスクを実行するでしょう。

cap search_libs

なるほど、では、capfile をメモ帳で作成し、そこのカレントディレクトリにコマンドプロンプトで移動して、 cap search_libs と打てばいいのか。やってみよう。

task :search_libs, :hosts => "www.xxxxxxx.com" do
  run "ls -x1 /usr/lib | grep -i xml"
end

※www.xxxxxxx.com は実際はちゃんとリモートするサーバーのドメインが入っています。

  • 実行してみた
C:\capistrano_example>cap search_libs
the task `search_libs' does not exist

(;゚ Д゚)
・・・・・・・・ファイル作って、書いたはいいけど、どうやって実行するんだ?(´・ω・`)

ということで、コマンドラインヘルプを見てみる。

C:\capistrano_example>cap -h
Usage: cap [options] action ...
   -d, --debug                      Prompts before each remote command execution.
   -e, --explain TASK               Displays help (if available) for the task.
   -F, --default-config             Always use default config, even with -f.
   -f, --file FILE                  A recipe file to load. May be given more than once.
   -H, --long-help                  Explain these options.
   -h, --help                       Display this help message.
   -p, --password                   Immediately prompt for the password.
   -q, --quiet                      Make the output as quiet as possible.
   -S, --set-before NAME=VALUE      Set a variable before the recipes are loaded.
   -s, --set NAME=VALUE             Set a variable after the recipes are loaded.
   -T, --tasks                      List all tasks in the loaded recipe files.
   -V, --version                    Display the Capistrano version, and exit.
   -v, --verbose                    Be more verbose. May be given more than once.
   -X, --skip-system-config         Don't load the system config file (capistrano.conf)
   -x, --skip-user-config           Don't load the user config file (.caprc)

なるほど、 -f でファイルを指定するのか。やってみよう。


C:\capistrano_example>cap -f capfile search_libs
 * executing `search_libs'
 * executing "ls -x1 /usr/lib | grep -i xml"
   servers: ["www.xxxxxxx.com"]
Password:
connection failed for: www.xxxxxxx.com (Net::SSH::AuthenticationFailed: 福重伸太朗)

(;゚ Д゚)
もしかして、windowsでログインしているユーザー名でアクセスしようとしていませんか・・・・・?


  • set :user でユーザーを指定する

set :user でユーザー名が指定できるらしい>< nag++

set :user, "your_user_name"
task :search_libs, :hosts => "www.xxxxxxx.com",:user =>"your_user_name" do
 run "ls -x1 /usr/lib | grep -i xml"
end
  • 再度実行
C:\capistrano_example>cap -f capfile search_libs
 * executing `search_libs'
 * executing "ls -x1 /usr/lib | grep -i xml"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] libxml2.a
** [out :: www.xxxxxxx.com] libxml2.so
** [out :: www.xxxxxxx.com] libxml2.so.2
** [out :: www.xxxxxxx.com] libxml2.so.2.6.27
** [out :: www.xxxxxxx.com] xml2Conf.sh
   command finished

いけたー"o(>ω< )o"
ちゃんと、出力情報がとれたお><

You can define as many tasks as you want:

すきなだけ、task を追加できます。おおぉ。

task :search_libs, :hosts => "www.xxxxxxx.com" do
  run "ls -x1 /usr/lib | grep -i xml"
end

task :count_libs, :hosts => "www.xxxxxxx.com" do
  run "ls -x1 /usr/lib | wc -l"
end

例では、こんな感じで書いてますね。

Here we’ve added a second task, “count_libs”, which will display the number of entries in /usr/lib. We could add more, but even with just two tasks, having to specify the host over and over is getting a little unwieldy. Here is where “roles” come into play:

なるほど、task が増えると、何回も同じ hots を書かないといけなくて、面倒だといいたいのですね。

role :libs, "www.xxxxxxx.com"

task :search_libs do
  run "ls -x1 /usr/lib | grep -i xml"
end

task :count_libs do
  run "ls -x1 /usr/lib | wc -l"
end

だから、↑こう書けるよということですね。

(Note: Capistrano’s login defaults to whatever user you are currently logged into your local machine as. If you need to log in as a different user, you can encode that username in the server definition, “joe@www.capify.org”. Alternatively, you can set the :user variable to the username you want to use. We’ll get to variables in a minute.)

なるほど。ちゃんと、書いてあった>< もうちょっと前にかくべきなんだお><

task :search_libs, :hosts => "your_user_name@www.xxxxxxx.com" do
  run "ls -x1 /usr/lib | grep -i xml"
end

これでもいけるし、nagさんに教えてもらった、

set :user, "your_user_name"

task :search_libs, :hosts => "www.xxxxxxx.com" do
  run "ls -x1 /usr/lib | grep -i xml"
end

これでも、いけるってことですね。


Gateway servers

In the “real world”, we have to worry about bad guys trying to sneak into our servers, so many server clusters are hidden behind NATs and firewalls, to prevent direct access. Instead, you have to log into some “gateway” server, and then log into the servers you want, from there.

悪さする奴がいるから、ゲートウェイサーバーにログインして、そこからサーバにログインしなければなりませんと書いてあるが・・・・。
なるほど、特定のゲートウェイを通過して確立した接続のみ操作できるという設定・・・だと思うのですが、l
まずは、必要なさそうなので、とりあえずあることだけ覚えておこう。l


Multiple servers

role :libs, "crimson.capify.org", "magenta.capify.org"

これで、2つのサーバーに並行して命令を実行できるよ!って書いてあると思う。

role :libs, "www.xxxxxxx.com","www2.xxxxxxx.com"

task :search_libs do
  run "ls -x1 /usr/lib | grep -i xml"
end

task :count_libs do
  run "ls -x1 /usr/lib | wc -l"
end

たとえば、これで "www.xxxxxxx.com","www2.xxxxxxx.com" に並行して命令を飛ばせるわけですね。
ただし、"www.xxxxxxx.com","www2.xxxxxxx.com" に ssh でログインできるユーザーとパスワードは同じものでないといけません。


Multiple roles

role :libs, "www.xxxxxxx.com"
role :files, "www2.xxxxxxx.com"

task :search_libs, :roels => :libs do
  run "ls -x1 /usr/lib | grep -i xml"
end

task :count_libs, :roels => :files do
  run "ls -x1 /usr/lib | wc -l"
end

このように、役割を付けてあげれば、サーバーごとに、分けて実行できますよ!と書いてあると思う。
これなら、ユーザーとパスワードは同じでなくても大丈夫ですね。



Documenting tasks

それぞれの、tasksに説明を付けることができます。
tasksの名前は、自由に付けることができますので、それで分かればよいですが、分かりにくい場合は説明を書くとトができます。
それを表示するのが、 cat -T です。やってみましょう。

role :libs, "www.xxxxxxx.com"
role :files, "www2.xxxxxxx.com"

desc "Search /usr/lib for files named xml."
task :search_libs, :roles => :libs do
 run "ls -x1 /usr/lib | grep -i xml"
end

desc "This is a test task."
task :test_tasks, :roles => :files do
 run "ls -x1 /usr/lib | wc -l"
end
  • 実行結果
C:\capistrano_example>cap -T
cap invoke      # Invoke a single command on the remote servers.
cap search_libs # Search /usr/lib for files named xml.
cap shell       # Begin an interactive Capistrano session.
cap test_tasks  # This is a test task.

Extended help may be available for these tasks.
Type `cap -e taskname' to view it.

おおおぉ。説明が追加されていますね。
ただし、同じタスク名が存在した場合は、ひとつしか表示しません。



また、ひとつのタスクだけ見ることができます。-e オプションを使います。

C:\capistrano_example>cap -e search_libs
------------------------------------------------------------
cap search_libs
------------------------------------------------------------
Search /usr/lib for files named xml.

cap invoke

ファイルに task を記述しなくても、 invoke を使えば呼び出せちゃうよ!ってもの。

role :libs, "www.xxxxxxx.com"
  • 実行結果
C:\capistrano_example>cap invoke COMMAND="df -h"
 * executing `invoke'
 * executing "df -h"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] Filesystem            Size  Used Avail Use% Mounted on
** [out :: www.xxxxxxx.com] /dev/simfs             20G  5.2G   15G  26% /
   command finished

C:\capistrano_example>

roleが複数ある場合は、ROLES をしていすれば、見るサーバーを指定できるよ!

role :libs, "www.xxxxxxx.com"
role :libs_2", "www2.xxxxxxx.com"
  • 実行結果
C:\capistrano_example>cap invoke COMMAND="df -h" ROLES=libs_2
 * executing `invoke'
 * executing "df -h"
   servers: ["www2.xxxxxxx.com"]
Password:
   [www2.xxxxxxx.com] executing command
** [out :: www2.xxxxxxx.com] Filesystem            Size  Used Avail Use% Mounted on
** [out :: www2.xxxxxxx.com] /dev/simfs             20G  10.0G   15G  50% /
   command finished

C:\capistrano_example>


そして、なんと capfile なんてなくても実行できるよ!

空っぽ

  • 実行結果
C:\capistrano_example>cap invoke COMMAND="df -h" HOSTS=your_user_name@www.xxxxxxx.com
 * executing `invoke'
 * executing "df -h"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] Filesystem            Size  Used Avail Use% Mounted on
** [out :: www.xxxxxxx.com] /dev/simfs             20G  5.2G   15G  26% /
   command finished

C:\capistrano_example>

cap shell

たしかに、cap invoke はいいけど、毎回コネクションをやり直すので、色々なシェルを何回も行うのは苦痛でしょ?ってことで、cap shell がある。

C:\capistrano_example>cap shell
 * executing `shell'
====================================================================
Welcome to the interactive Capistrano shell! This is an experimental
feature, and is liable to change in future releases. Type 'help' for
a summary of how to use the shell.
--------------------------------------------------------------------
cap> df -h
[establishing connection(s) to www.xxxxxxx.com]
Password:
** [out :: www.xxxxxxx.com] Filesystem            Size  Used Avail Use% Mounted on
** [out :: www.xxxxxxx.com] /dev/simfs             20G  5.2G   15G  26% /
cap>

一度ログインしてしまえば、Linuxにログインしていのと同じような感じで操作できる。すごいわー。


Namespaces

task を分類することができます。

role :libs, "www.xxxxxxx.com"
set :user, "your_user_name"

namespace :libs do
 desc "Search /usr/lib for files named xml."
 task :search, :roles => :libs do
   run "ls -x1 /usr/lib | grep -i xml"
 end

 desc "counte test"
 task :count, :roles => :libs do
   run "ls -x1 /usr/lib | wc -l"
 end
end


libs:search このように実行します。

  • 実行結果
C:\capistrano_example>cap -f capfile libs:search
 * executing `libs:search'
 * executing "ls -x1 /usr/lib | grep -i xml"
   servers: ["www.xxxxxxx.com"]
Password:
   [wwww.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] libxml2.a
** [out :: www.xxxxxxx.com] libxml2.so
** [out :: www.xxxxxxx.com] libxml2.so.2
** [out :: www.xxxxxxx.com] libxml2.so.2.6.27
** [out :: www.xxxxxxx.com] xml2Conf.sh
   command finished


また、namespace のデフォルト task を設定できます。
そうすると、 libs だけで実行できます。

role :libs, "www.xxxxxxx.com"
set :user, "your_user_name"

namespace :libs do
 desc "Search /usr/lib for files named xml."
 task :default, :roles => :libs do
   run "ls -x1 /usr/lib | grep -i xml"
 end

 desc "counte test"
 task :count, :roles => :libs do
   run "ls -x1 /usr/lib | wc -l"
 end
end
  • 実行結果
C:\capistrano_example>cap -f capfile libs
 * executing `libs'
 * executing "ls -x1 /usr/lib | grep -i xml"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] libxml2.a
** [out :: www.xxxxxxx.com] libxml2.so
** [out :: www.xxxxxxx.com] libxml2.so.2
** [out :: www.xxxxxxx.com] libxml2.so.2.6.27
** [out :: www.xxxxxxx.com] xml2Conf.sh
   command finished

Variables

なんと、変数も設置できます。

role :libs, "www.xxxxxxx.com"
set :user, "your_user_name"
set :term, "xml"

task :search, :roles => :libs do
 run "ls -x1 /usr/lib | grep -i #{term}"
end
  • 実行結果
C:\capistrano_example>cap -f capfile libs
 * executing `libs'
 * executing "ls -x1 /usr/lib | grep -i xml"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] libxml2.a
** [out :: www.xxxxxxx.com] libxml2.so
** [out :: www.xxxxxxx.com] libxml2.so.2
** [out :: www.xxxxxxx.com] libxml2.so.2.6.27
** [out :: www.xxxxxxx.com] xml2Conf.sh
   command finished


しかも、コマンドライン入力時に、対話型で入力することもできます。

role :libs, "www.xxxxxxx.com"
set :user, "your_user_name"
set(:term) do
 print "Gimme a search term: "
 STDOUT.flush
 STDIN.gets.chomp
end

task :search, :roles => :libs do
 run "ls -x1 /usr/lib | grep -i #{term}"
end
  • 実行結果
C:\capistrano_example>cap -f capfile search
 * executing `search'
Gimme a search term: xml
 * executing "ls -x1 /usr/lib | grep -i xml"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] libxml2.a
** [out :: www.xxxxxxx.com] libxml2.so
** [out :: www.xxxxxxx.com] libxml2.so.2
** [out :: www.xxxxxxx.com] libxml2.so.2.6.27
** [out :: www.xxxxxxx.com] xml2Conf.sh
   command finished


もっと簡単にかけます。

role :libs, "www.xxxxxxx.com"
set :user, "your_user_name"
set(:term) do
 Capistrano::CLI.ui.ask "Gimme a search term: "
end

task :search, :roles => :libs do
 run "ls -x1 /usr/lib | grep -i #{term}"
end
  • 実行結果
C:\capistrano_example>cap -f capfile search
 * executing `search'
Gimme a search term: xml
 * executing "ls -x1 /usr/lib | grep -i xml"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] libxml2.a
** [out :: www.xxxxxxx.com] libxml2.so
** [out :: www.xxxxxxx.com] libxml2.so.2
** [out :: www.xxxxxxx.com] libxml2.so.2.6.27
** [out :: www.xxxxxxx.com] xml2Conf.sh
   command finished


-s オプションで最初から、指定して渡せます。

C:\capistrano_example>cap -f capfile -s term=xml search
 * executing `search'
 * executing "ls -x1 /usr/lib | grep -i xml"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] libxml2.a
** [out :: www.xxxxxxx.com] libxml2.so
** [out :: www.xxxxxxx.com] libxml2.so.2
** [out :: www.xxxxxxx.com] libxml2.so.2.6.27
** [out :: www.xxxxxxx.com] xml2Conf.sh
   command finished


そのほかもろもろ、いろんなオプションが使えます。

C:\capistrano_example>cap -h
Usage: cap [options] action ...
   -d, --debug                      Prompts before each remote command execution.
   -e, --explain TASK               Displays help (if available) for the task.
   -F, --default-config             Always use default config, even with -f.
   -f, --file FILE                  A recipe file to load. May be given more than once.
   -H, --long-help                  Explain these options.
   -h, --help                       Display this help message.
   -p, --password                   Immediately prompt for the password.
   -q, --quiet                      Make the output as quiet as possible.
   -S, --set-before NAME=VALUE      Set a variable before the recipes are loaded.
   -s, --set NAME=VALUE             Set a variable after the recipes are loaded.
   -T, --tasks                      List all tasks in the loaded recipe files.
   -V, --version                    Display the Capistrano version, and exit.
   -v, --verbose                    Be more verbose. May be given more than once.
   -X, --skip-system-config         Don't load the system config file (capistrano.conf)
   -x, --skip-user-config           Don't load the user config file (.caprc)

Transactions

なんと、トランザクションも使えます。処理が失敗したら、ロールバックしてくれるんです。
とはいっても、自分で記述することで、ロールバックを実現します。
もし、なにか不具合が起こった場合、on_rollback が実行されます。

role :libs, "www.xxxxxxx.com"
set :user, "your_user_name"

task :test_task do
 transaction do
   make_test_file
 end
end

task :make_test_file, :roles => :libs do
 on_rollback { run "rm -f test_file" }
 run "touch test_file"
 run "echo 'aaaaaa' > test_file"
end
  • 実行結果
C:\capistrano_example>cap -f capfile test_task
 * executing `test_task'
** transaction: start
 * executing `make_test_file'
 * executing "touch test_file"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
   command finished
 * executing "echo 'aaaaaa' > test_file"
   servers: ["www.xxxxxxx.com"]
   [www.xxxxxxx.com] executing command
   command finished
** transaction: commit

Loading files

capfile が複雑になってきたら、ファイルを分けて、そのファイルをロードしたりできます。そうしてカプセル化するなどできます。
ファイルのロードパスはデフォルトは、カレントディレクトを探すでしょう。
もし、ロードパスを追加したいなら、ロードパスを追加することもできます。

role :libs, "www.xxxxxxx.com"
set :user, "your_user_name"
load "capfile2"
load_paths << "C:/capistrano_example/files"
load "capfile3"

task :search_libs do
 run "ls -x1 /usr/lib | grep -i xml"
end
task :count_libs do
 run "ls -x1 /usr/lib | wc -l"
end
  • ファイル : C:\capistrano_example\files\capfile3
task :hdd_check do
 run "df -h"
end
  • 実行結果 capfile2
C:\capistrano_example>cap -f capfile count_libs
 * executing `count_libs'
 * executing "ls -x1 /usr/lib | wc -l"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] 775
   command finished
  • 実行結果 capfile3
C:\capistrano_example>cap -f capfile hdd_check
 * executing `hdd_check'
 * executing "df -h"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] Filesystem            Size  Used Avail Use% Mounted on
** [out :: www.xxxxxxx.com] /dev/simfs             20G  5.2G   15G  26% /
   command finished

That's it!

これらは、カピストラーノの基本機能です。他の多くの特徴は別のチュートアルに記述されているでしょう。


次回

Railsのデプロイをやってみます。

libdpstk.so.1: cannot open shared object file: No such file or directory - /usr/lib/ruby/gems/1.8/gems/rmagick-1.13.0/lib/RMagick.so

what

突然、以下のエラーが出始めた。今まで出なかったのに・・・。

# rake
(in /home/bar/fuga)
rake aborted!
libdpstk.so.1: cannot open shared object file: No such file or directory - /usr/lib/ruby/gems/1.8/gems/rmagick-1.13.0/lib/RMagick.so

(See full trace by running task with --trace)


なんぞこれ・・・orz


エラーを詳しく見る

# rake --trace
(in /rails/fuga)
** Invoke default (first_time)
** Invoke test (first_time)
** Execute test
** Invoke test:units (first_time)
** Invoke db:test:prepare (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
libdpstk.so.1: cannot open shared object file: No such file or directory - /usr/lib/ruby/gems/1.8/gems/rmagick-1.13.0/lib/RMagick.so
/usr/lib/ruby/gems/1.8/gems/rmagick-1.13.0/lib/RMagick.so
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:32:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require'
/usr/lib/ruby/gems/1.8/gems/rmagick-1.13.0/lib/RMagick.rb:11
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:32:in `gem_original_require'
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:32:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require'
/home/livep395/rixi/config/../config/environment.rb:98
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
/usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/misc.rake:3
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `call'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `execute'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `execute'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:362:in `invoke'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in `synchronize'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in `invoke'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:369:in `invoke_prerequisites'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1003:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1003:in `send'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1003:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:368:in `invoke_prerequisites'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:361:in `invoke'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in `synchronize'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in `invoke'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:369:in `invoke_prerequisites'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1003:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1003:in `send'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1003:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:368:in `invoke_prerequisites'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:361:in `invoke'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in `synchronize'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in `invoke'
/usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/testing.rake:45
/usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/testing.rake:43:in `collect'
/usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/testing.rake:43
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `call'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `execute'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `execute'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:362:in `invoke'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in `synchronize'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in `invoke'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:369:in `invoke_prerequisites'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1003:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1003:in `send'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1003:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:368:in `invoke_prerequisites'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:361:in `invoke'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in `synchronize'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in `invoke'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:in `standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1733:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1711:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:in `standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1708:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/bin/rake:7
/usr/bin/rake:16:in `load'
/usr/bin/rake:16


/usr/lib/ruby/gems/1.8/gems/rmagick-1.13.0/lib/RMagick.rb:11

↑ここをみると

require 'RMagick.so'

となっていて、これがエラーする。


つまり、以下が解決できればいい。

# irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'RMagick.so'
LoadError: no such file to load -- RMagick.so
       from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
       from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
       from (irb):3
irb(main):004:0>

これが、

irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'RMagick.so'
=> true

こうなるようにしよう。


RMagickの Install FAQ を見てみる

http://rmagick.rubyforge.org/install-faq.html#loaderror
↑ここが一番あやしいけど、

export LD_LIBRARY_PATH=/usr/local/lib

やってもだめ・・・・orz



ImageMagick と RMagick を再インストール

色々調べた結果・・・分からず・・・orz
もう・・・再インストールしかない・・・。

  • RMagick のアンインストール
# gem uninstall rmagick
# rpm -qa | grep ImageMagick
ImageMagick-perl-6.0.7.1-16
ImageMagick-devel-6.0.7.1-16
ImageMagick-6.0.7.1-16
# rpm -e ImageMagick-perl-6.0.7.1-16
# rpm -e ImageMagick-devel-6.0.7.1-16
# rpm -e ImageMagick-6.0.7.1-16
# rpm -qa | grep ImageMagick
#
# yum install ImageMagick
# yum install ImageMagick-devel
# yum install ghostscript*
  • RMagick インストール
# gem install RMagick -v 1.13.0
Need to update 1 gems from http://gems.rubyforge.org
.
complete
Building native extensions.  This could take a while...
Successfully installed rmagick-1.13.0

おおおおおおぉ、ちゃんと入った!

# rpm -qa | grep ImageMagick
ImageMagick-devel-6.0.7.1-16
ImageMagick-6.0.7.1-16
# gem list | grep rmagick
rmagick (1.13.0)

ImageMagick-perl-6.0.7.1-16 はいまのところ必要ないのでインストールはしませんでした。

  • ちゃんと require できるか確認する
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'RMagick.so'
=> true


できましたー。

しかし、根本的な原因が分からず・・・。
なぜ、とつぜん動かなくなるのか・・・。

つっこみ歓迎です。

環境

# ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-linux]

irbを便利につかうために for windows

Route 477(2007-08-01)
↑こちらを参考に、まずは

プログラミング言語 Ruby リファレンスマニュアル
↑completion機能をwindowsirb起動時に使えるようにしましょう。

irb実行中に(TAB)を押すとコンプレーション(入力補完)してくれる機能ですね。


まず、irbについて

こちらを見る。
プログラミング言語 Ruby リファレンスマニュアル


最初に、結論

windowsirb は .irbrc の場所を知らないようなので、指定します。

C:\>irb
irb(main):001:0> IRB.conf[:IRB_RC] = "c:\.irbrc"
=> "c:.irbrc"

それで、 c:\.irbrc というファイルを作り、設定します。

  • c:\.irbrc
require "irb/completion"

これで irb 起動時に入力補完機能が使えるようになります。
あとは、いろいろ追加していけばおkですね。

※.irbrcの場所はご自由にどうぞ。




ということで、以下は試行錯誤の過程を観たい方はどうぞ。




windows の .irbrc はどこだ?

Route 477(2007-08-01)
こちらにホームディレクトリは、

ruby -e 'p ENV["HOME"]'で出てくるディレクト

と書いていあるのですが、windowsで行うと、

C:\>ruby -e 'print ENV["HOME"]'
nil

nil なんです。。


[Ruby] irbにおける文字化け -Rubyを勉強を始めようと思いまして、Acti- その他(プログラミング・Web制作) | 教えて!goo
↑こちらに

.irbrc の置き場所などはマニュアルを参照してください。

Rubyリファレンスマニュアル - irb
http://www.ruby-lang.org/ja/man/?cmd=view;name=irb​

~ というのはUnixで使われるシェルで用いられる、ユーザーの
ホームディレクトリを表すものです。
Windows版であれば、環境変数 HOMEの値が用いられます。

~/.irbrc

%HOME%/.irbrc

のように読み替えてください。
この環境変数は通常は設定されませんので、ご自分の環境に合わせて
設定してください
(HOMEDRIVEとHOMEPATHならあらかじめ設定されていると思います)。

と書いてありますので、

C:\>ruby -e 'print ENV["HOMEPATH"]'
\Documents and Settings\福重伸太朗
C:\>ruby -e 'print ENV["USERPROFILE"]'
C:\Documents and Settings\福重伸太朗

らしい。


windows の .irbrc は読み込まれているのか?

irb起動時に .irbrc は読み込まれているのでしょうか?

C:\>irb
irb(main):001:0> conf.rc
=> true

読みこまれているらしい。

では、

\Documents and Settings\福重伸太朗\.irbrc
があるのでしょうか・・・・・・・ない・・・・・。

ないのに、読み込まれているのでしょうか・・・。


とりあえず、

C:\Documents and Settings\福重伸太朗\.irbrc

というファイルを作り、中に

require "irb/completion"

を書いて irb を起動して、入力補完機能が使えるかやってみましたが、できませんでした・・・。

もちろん、irb起動中に

require "irb/completion"

すれば入力補完機能は使えるようになります。


うーん。windowsの .irbrc はどこにあるのでしょうか・・・。


IRB.conf[:IRB_RC] で .irbrc の場所をしいする

どうやら、.irbrc の場所を irb が知らないようです。なので、指定しましょう。

プログラミング言語 Ruby リファレンスマニュアル

こちらに、
IRB.conf[:IRB_RC]
というオプションがあります。
デフォルトでは nil っぽいです。

なので、

irb(main):004:0> IRB.conf[:IRB_RC] = "c:\.irbrc"
=> "c:.irbrc"

上記のように .irbrc の場所を指定して、

c:\.irbrc というファイルを作ります。

  • c:\.irbrc
require "irb/completion"


それで、再度トライ。

C:\>irb
irb(main):001:0> a = []
=> []
irb(main):002:0> a.in
a.include?                    a.instance_eval
a.index                       a.instance_of?
a.indexes                     a.instance_variable_defined?
a.indices                     a.instance_variable_get
a.inject                      a.instance_variable_set
a.insert                      a.instance_variables
a.inspect
irb(main):002:0> a.in

おおおおおぉ。入力補完機能が効くようになりました!
一件落着。


環境

C:\>ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]