iRSSの日記

はてなダイアリーiRSSの日記の続き

DreamHost上で、gemを追加する方法

jsonをgemで追加したくなったので、調べてみた


.gems を追加して.bash_profileに、いくつかの設定を追加する。
以下の作業は、ユーザーのhomeディレクトリで行うこと。

RubyGem自体は、共通のものを利用して、gemのインストール先を、変更するという方法。
RubyGem自体を、独自に、インストールする方法もあるけれど、こちらがお手軽では。

mkdir .gems
echo 'export GEM_HOME="$HOME/.gems"' >> .bash_profile
echo 'export GEM_PATH="$GEM_HOME:/usr/lib/ruby/gems/1.8"' >> .bash_profile
echo 'export PATH="$HOME/.gems/bin:$PATH"' >> .bash_profile

これで、jsonのインストールもできるようになった

Bulk updating Gem source index for: http://gems.rubyforge.org
Select which gem to install for your platform (i386-linux)
1. json 1.1.1 (mswin32)
2. json 1.1.1 (ruby)
3. json 1.1.0 (mswin32)
4. json 1.1.0 (ruby)
5. Skip this gem
6. Cancel installation
> 2
Building native extensions. This could take a while...
Successfully installed json-1.1.1
Installing ri documentation for json-1.1.1...

No definition for cState_configure

No definition for cState_configure
Killed

こんな風に、無事動作。

[lancer]$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'json'
=> true
irb(main):003:0> a = {"a" => [1,2,3] ,"b" => "hello"}
=> {"a"=>[1, 2, 3], "b"=>"hello"}
irb(main):004:0> j = a.to_json
=> "{\"a\":[1,2,3],\"b\":\"hello\"}"
irb(main):005:0> JSON::parse(j)
=> {"a"=>[1, 2, 3], "b"=>"hello"}
irb(main):006:0>

cgiで動かすときは、注意

コンソールで、動かしているときは、問題なかったのだけど、apacheはどうも、ローカルのパスを理解してくれない。

[Sun Sep 23 05:45:07 2007] [error] [client 61.245.47.134] /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:
in `gem_original_require': no such file to load -- json (LoadError)

こんな、エラーが出ています。
パスが通らないんだなあ。

Also, if you are using the Ruby on Rails platform, your "require" statements should list the full path of your personally installed rubygem. IE: require '/home/your_account/.gems/gems/activemerchant-0.6.0/lib/active_merchant'

結局↓こうやって、解消。ちょと、かっこ、わるいですが...

#!/usr/bin/ruby
$LOAD_PATH << "/home/[user名]/.gems/gems/json-1.1.1/lib/"
require 'rubygems'
require "json"