rubyの最近のブログ記事

マルチユーザ用にRVMをインストール

マルチユーザ用にRVMをインストールするには、sudo でインストールする。

RVM: Ruby Version Manager - Installing RVM

$ sudo bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )
Downloading RVM from wayneeseguin branch stable

curl: (60) SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). The default
 bundle is named curl-ca-bundle.crt; you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.

Could not download 'https://github.com/wayneeseguin/rvm/tarball/stable'.
  Make sure your certificates are up to date as described above.
  To continue in insecure mode run 'echo insecure >> ~/.curlrc'.

curlのSSL証明書の検証でエラーになった。
CA証明書が古いらしい。
新しいCA証明書を取得する。

CA証明書のアップデート

$ sudo mv /etc/pki/tls/certs/ca-bundle.crt /etc/pki/tls/certs/ca-bundle.crt.bak # バックアップ
$ sudo curl http://curl.haxx.se/ca/cacert.pem -o /etc/pki/tls/certs/ca-bundle.crt

curlでまたSSLのエラーが出たので対処 - kanonjiの日記
cURL - Details on Server SSL Certificates
cURL - Extract CA Certs from Mozilla

再度sudoでRVMをインストール。

$ sudo bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )
Downloading RVM from wayneeseguin branch stable
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  796k  100  796k    0     0   170k      0  0:00:04  0:00:04 --:--:--  340k

Installing RVM to /usr/local/rvm/
installing - /usr/local/rvm/man/man1/rvm.1 -
installing - /usr/local/rvm/man/man1/rvm.1.gz -
    Creating RVM system user group 'rvm'

# RVM:  Shell scripts enabling management of multiple ruby environments.
# RTFM: https://rvm.beginrescueend.com/
# HELP: http://webchat.freenode.net/?channels=rvm (#rvm on irc.freenode.net)
# Screencast: http://screencasts.org/episodes/how-to-use-rvm

# In case of any issues read output of 'rvm requirements' and/or 'rvm notes'

Installation of RVM in /usr/local/rvm/ is complete.

# XXXXXXXXX,
#
#   Thank you for using RVM!
#   I sincerely hope that RVM helps to make your life easier and more enjoyable!!!
#
# ~Wayne

sudoでマルチユーザ用にインストールすると、RVMは /usr/local/rvm にインストールされる。

sudoを使うのは、RVM自体のインストール時のみ。

以降の操作は、sudoを使わない。rvm installやgem install など、/usr/local/rvm 下に書き込む操作は、rvmグループに所属しているユーザで行う。(/usr/local/rvmのグループがrvmになっている。)

RVM: Ruby Version Manager - Troubleshooting RVM
RVM をマルチユーザー用にセットアップする: ひ日記

ユーザのグループにrvmを追加。

# usermod -a -G rvm <ユーザ名>

ユーザをrvmグループに追加したら、いったんログアウトしてから、rubyやgemをインストール。
sudo しない。

$ rvm install 1.9.3
$ rvm use 1.9.3
$ gem install rails

RVMのアップデートも、sudoを使わず、rvmsudoを使う。

$ rvmsudo rvm get latest

カテゴリー:

ImageMagick 6.7.3-7をソースからインストールした環境に、gemでRMagickをインストールしようとしたら、以下のエラーが発生した。

$ sudo gem install rmagick
Building native extensions.  This could take a while...
ERROR:  Error installing rmagick:
	ERROR: Failed to build gem native extension.

        /opt/ruby-enterprise-1.8.7-2011.03/bin/ruby extconf.rb
checking for Ruby version >= 1.8.5... yes
checking for gcc... yes
checking for Magick-config... yes
checking for ImageMagick version >= 6.4.9... yes
checking for HDRI disabled version of ImageMagick... yes
Package MagickCore was not found in the pkg-config search path.
Perhaps you should add the directory containing `MagickCore.pc'
to the PKG_CONFIG_PATH environment variable
No package 'MagickCore' found
Package MagickCore was not found in the pkg-config search path.
Perhaps you should add the directory containing `MagickCore.pc'
to the PKG_CONFIG_PATH environment variable
No package 'MagickCore' found
Package MagickCore was not found in the pkg-config search path.
Perhaps you should add the directory containing `MagickCore.pc'
to the PKG_CONFIG_PATH environment variable
No package 'MagickCore' found
Package MagickCore was not found in the pkg-config search path.
Perhaps you should add the directory containing `MagickCore.pc'
to the PKG_CONFIG_PATH environment variable
No package 'MagickCore' found
checking for stdint.h... yes
checking for sys/types.h... yes
checking for wand/MagickWand.h... no

Can't install RMagick 2.13.1. Can't find MagickWand.h.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/opt/ruby-enterprise-1.8.7-2011.03/bin/ruby


Gem files will remain installed in /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/gems/1.8/gems/rmagick-2.13.1 for inspection.
Results logged to /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/gems/1.8/gems/rmagick-2.13.1/ext/RMagick/gem_make.out

ImageMagickは6.7.3-7をソースからインストールしている。
ImageMagick 6.6.9-10の場合は、Rmagickをインストールできた。

対処方法

エラーメッセージに、

Perhaps you should add the directory containing `MagickCore.pc'
to the PKG_CONFIG_PATH environment variable

とあるので、MagickCore.pcが含まれているディレクトリを探して、PKG_CONFIG_PATHにセットするとインストールできた。

# find /usr/local -name MagickCore.pc
/usr/local/lib/pkgconfig/MagickCore.pc
# PKG_CONFIG_PATH=/usr/local/lib/pkgconfig gem install rmagick
Building native extensions.  This could take a while...
Successfully installed rmagick-2.13.1
1 gem installed
Installing ri documentation for rmagick-2.13.1...
Installing RDoc documentation for rmagick-2.13.1...

カテゴリー:

カテゴリー:

Mac OSX Lion で RVMでインストールしたRuby 1.8.7 で、

$ gem install rails

などとやると、

ruby-1.8.7-p352/lib/ruby/1.8/timeout.rb:60: [BUG] Segmentation fault

というエラーになってしまう。

その解決方法。

.bash_profileなどに、

export CC=gcc-4.2

とやっておいてから、RVMでRuby をインストールする。

ただし、後述するように、Xcode 4.2.1にはgccが含まれていない。別途gccを入れる必要がある。

私は、.bash_profileへの追加はせずに、RVMでのRubyインストール時に、
$ CC=/usr/bin/gcc-4.2 rvm install 1.8.7

とした。

Xcode 4.2.1 にはgcc-4.2が入っていないので、gcc-4.2を別途インストールする必要がある。
rvm requirements に記載があった。

まず、kennethreitz/osx-gcc-installer - GitHub でgccをインストール。
その後、必要があれば、Xcode 4.2.1をインストールすればよい。


※ 1.9.2では、上記の問題は発生しなかった。

※ Snow Leopard では、RVMでのRubyインストール時に、

$ rvm pkg install readline
$ rvm install 1.9.2 --with-readline-dir=$rvm_path/usr

とやらないと、irbで日本語が入力できなかったが、Lionではreadlineの互換ライブラリlibeditが改善され、ふつうにインストールすれば大丈夫になった。

RVM, Ruby 1.8.7 and OSX Lion « frymanet.com

カテゴリー:

require 'open-uri'
require 'resolv-replace'
require 'timeout'

TIMEOUT = 3

begin
  timeout(TIMEOUT) do
    open(url) do |f|
      # 処理
    end
  end
rescue TimeoutError => e
  # タイムアウト時の例外処理
rescue => e
  # その他の例外処理
end
  • timeout による割り込みはRubyのThreadによって実現されており、C言語レベルで実装されRuby のスレッドが割り込めない処理では無効なので、SocketでDNSの名前解決に時間がかかった場合にタイムアウトしない。そのため、resolvライブラリを使うようにresolv-replaceでメソッドの書き換えを行う。
  • タイムアウト時のTimeoutErrorはStandardErrorのサブクラスではないので、捕捉するためには明示的に捕捉するクラス(TimeoutError)をrescueに指定する必要がある。

open-uriにtimeoutを設定する方法 | やむにやまれず

カテゴリー:

RMagickでEXIFの情報を取得する。
Using RMagick and EXIF to get the date a photo is taken in Rails « i am josh

EXIFのOrientationタグの値を元に画像を回転する。
オレBlog » Exif Orientationタグによる補正方法

カテゴリー:

カテゴリー:

カテゴリー:

libHaruはPDFを生成するCのライブラリ。
Ruby、Delphi/Free Pascal、C#(Microsoft .NET platform)、PHP、Perlから利用可能にするbindingもある。
libHaru.org wiki

ダウンロードページからソースをダウンロードしてインストールする。
libpngはMacPortsでインストールしているので、--with-png=/opt/local を指定。

$ tar xzvf libharu-2.1.0.tar.gz
$ cd libharu-2.1.0/
$ ./configure --with-png=/opt/local
$ make
$ sudo make install

Ruby用のライブラリをインストールする。
Documentation/Bindings - libHaru.org wiki

$ cd if/ruby/
$ ruby extconf.rb
$ sudo make install

カテゴリー:

Mac OSX (Snow Leopard) で、MacPortsのRuby1.8.7を使っているが、1.9.2も使いたいので、RVMでRuby1.9.2をインストールしてみた。

RVMのインストール

$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile

RVM: Ruby Version Manager - Installing RVM

RVMのアップデート

$ rvm get latest

RVM: Ruby Version Manager - Upgrading RVM

rvm get latest がエラーになる場合は、
$ rvm get head
$ rvm get latest

Gamburtsev: rvm get latest が失敗する場合

Rubyのインストール

インストール可能なRubyの確認

$ rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.6-head
[ruby-]1.8.7[-p334]
[ruby-]1.8.7-head
[ruby-]1.9.1-p378
[ruby-]1.9.1[-p431]
[ruby-]1.9.1-head
[ruby-]1.9.2-preview1
[ruby-]1.9.2-preview3
[ruby-]1.9.2-rc1
[ruby-]1.9.2-rc2
[ruby-]1.9.2[-p180]
[ruby-]1.9.2-head
ruby-head

# GoRuby
goruby

# JRuby
jruby-1.2.0
jruby-1.3.1
jruby-1.4.0
jruby[-1.5.6]
jruby-1.6.0.RC1
jruby-1.6.0.RC2
jruby-head

# Rubinius
rbx-1.0.1
rbx-1.1.0
rbx-1.1.1
rbx-1.2.0
rbx-1.2.1
rbx[-head]

# Ruby Enterprise Edition
ree-1.8.6
ree[-1.8.7][-2011.03]
ree-1.8.6-head
ree-1.8.7-head

# MagLev
maglev[-25315]
maglev-head

# Mac OS X Snow Leopard Only
macruby[-0.8] # the default macruby
macruby-nightly
macruby-head      # Build from the macruby git repository

# IronRuby -- Not implemented yet.
ironruby-0.9.3
ironruby-1.0-rc2
ironruby-head

1.9.2をインストールする

$ rvm pkg install readline
$ rvm install 1.9.2 --with-readline-dir=$rvm_path/usr

これで[ruby-]1.9.2[-p180](現時点)がインストールされる。

※ rvm pkg で readline をインストールして、それをRubyインストール時に指定しているのは、irbで日本語が入力できなかったから。

最初、普通にrvm install 1.9.2 でインストールしたら、irbで日本語を入力すると、

ruby-1.9.2-p180 :001 > "???"
SyntaxError: (irb):1: invalid multibyte char (UTF-8)
(irb):1: invalid multibyte char (UTF-8)
	from /Users/pistolfly/.rvm/rubies/ruby-1.9.2-p180/bin/irb:16:in `
'

となってしまった。

Ruby 1.9 - Bug #550: irbで日本語を入力できない - Ruby Issue Tracking System
と同じような現象なので、rvm pkgでreadlineをインストールして、Rubyインストール時に指定すると、大丈夫になった。

RVM: Ruby Version Manager - 'rvm package install readline'

※ Mac OSX Lion ではreadline(の互換ライブラリlibedit)が改善されたらしく、RVMのreadlineを使わなくても大丈夫になっている。

使用するRubyを選択

$ rvm use 1.9.2
Using /Users/pistolfly/.rvm/gems/ruby-1.9.2-p180
$ ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.6.0]

RVMでインストールされているRubyを確認

$ rvm list

rvm rubies

=> ruby-1.9.2-p180 [ x86_64 ]

デフォルトの Rubyを設定する

デフォルトのRubyを設定する。
$ rvm --default use 1.8.7
デフォルトのRubyに変更。
$ rvm default
デフォルトの設定を確認。
$ rvm list default

Default Ruby (for new shells)

   ruby-1.8.7-p352 [ x86_64 ]
デフォルトの設定をリセットする。
$ rvm reset

RVM: Ruby Version Manager - 'rvm default' - setting default ruby for new terminals

システムのRubyに戻す

$ rvm system

Rubyのアップグレード

RVM: Ruby Version Manager - Upgrading RVM.

例: 1.9.2-p180を1.9.2-p290にアップグレードする
$ rvm upgrade 1.9.2-p180 1.9.2-p290

screenで使う場合

~/.screenrcに以下を記述する。
shell -${SHELL}

RVM: Ruby Version Manager - RVM inside Screen

RubyGems

gemを使う時にsudoしないこと。

RVM: Ruby Version Manager - 'rvm gemdir' - RubyGems and RVM

Ruby 1.9.2 にruby-debugをインストール

Ruby 1.9.2 では「ruby-debug19」をインストールする。
$ gem install ruby-debug19

Ruby 1.9.2 にmongrelをインストール

Ruby 1.9.2 では、現時点では、1.2.0.pre2をインストールする。

mongrel | RubyGems.org | your community gem host

$ gem install mongrel -v 1.2.0.pre2 --pre

カテゴリー: